android - 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>
解决方案
由于您使用的是旧样式Widget.Design.TextInputLayout
,因此它不支持*.OutlinedBox
和*.FilledBox
样式具有的所有文本字段功能。
您不能errorIconDrawable
在 xml 中设置,您必须TextInputLayout.setErrorIconDrawable
在TextInputLayout.setError
.
login_password_input_layout.setError("Error text!!");
login_password_input_layout.setErrorIconDrawable(R.drawable....);
推荐阅读
- android - 如何在 firefox-beta android 上安装我的 .xpi
- haskell - 无法加载模块“System.Random”
- python - 使用 csv 模块和 datetime 模块解析时间戳
- c++ - 如何创建字符串的动态数组,因为该数组是 C++ 中结构的成员?
- html - 我的画廊底部有空白区域
- c# - 我不能在我的 WixSharp 安装程序中使用 RestSharp,是否可以使用 HttpClient 或 WebClient 完成此操作?
- javascript - 在数据匹配的数组中合并 JavaScript 对象
- javascript - 允许在 Typescript 的内置 JSX 属性中为 null?
- javascript - 有没有办法在不阻塞服务器的情况下运行 D3.js 以响应使用 Node/Express 的 http 请求?
- c - Linux init.h 库的替代品是什么