首页 > 解决方案 > TextInputLayout 不会设置 errorIconDrawable

问题描述

当我使用style="@style/Widget.Design.TextInputLayout"forTextInputLayout时,设置app:errorIconDrawable什么也不做。它仅在我不设置样式并让它继承应用程序主题(Theme.MaterialComponents.Light.NoActionBar)时才有效。app:endIconDrawable也不起作用,我找不到这个问题的替代方案/解决方案。请帮忙!


继承应用程序主题时以下工作:Theme.MaterialComponents.Light.NoActionBar但我不想要这种风格,特别是下划线与文本不对齐:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/login_password_input_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="username"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/login_password_input_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</com.google.android.material.textfield.TextInputLayout>

在此处输入图像描述


这使用了我需要的样式,但不会显示错误图标:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/login_password_input_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="username"
    style="@style/Widget.Design.TextInputLayout"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/login_password_input_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</com.google.android.material.textfield.TextInputLayout>

在此处输入图像描述

标签: androidmaterial-components-androidandroid-textinputlayouttextinputlayout

解决方案


由于您使用的是旧样式Widget.Design.TextInputLayout,因此它不支持*.OutlinedBox*.FilledBox样式具有的所有文本字段功能。

您不能errorIconDrawable在 xml 中设置,您必须TextInputLayout.setErrorIconDrawableTextInputLayout.setError.

login_password_input_layout.setError("Error text!!");                
login_password_input_layout.setErrorIconDrawable(R.drawable....);

在此处输入图像描述


推荐阅读