如何在XML布局中禁用endIconDrawable的默认着色模式?
解决TextInputLayout自定义endIconDrawable被默认着色变白的问题
针对你遇到的问题,给你几个可行的解决办法:
方法一:强制清除图标着色(布局+代码双保险)
你已经在布局里加了app:endIconTint="@null",但部分Material Components版本可能存在这个属性不生效的情况,建议再在代码里补一行:yourTextInputLayout.setEndIconTintList(null);这样能彻底禁用系统对自定义图标的默认着色,保留Drawable原本的灰色。
方法二:给Drawable选择器明确指定颜色
在你的password_toggle选择器文件里,给每个item的Drawable加上固定tint,确保颜色不会被覆盖:<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/ic_show_password" android:state_checked="true" android:tint="@color/your_gray_color"/> <item android:drawable="@drawable/ic_hide_password" android:state_checked="false" android:tint="@color/your_gray_color"/> </selector>这里的
your_gray_color替换成你原本Drawable的灰色值。方法三:自定义TextInputLayout样式
在styles.xml里创建一个自定义样式,覆盖默认的endIconTint:<style name="NoTintTextInputLayout" parent="Widget.MaterialComponents.TextInputLayout"> <item name="endIconTint">@null</item> </style>然后在布局里给TextInputLayout应用这个样式:
<com.google.android.material.textfield.TextInputLayout ... style="@style/NoTintTextInputLayout" app:endIconMode="custom" app:endIconDrawable="@drawable/password_toggle" ...>额外提示:配合数据绑定切换图标
要实现根据条件切换两个Drawable,直接在布局里用数据绑定绑定endIconDrawable即可,比如:app:endIconDrawable="@{viewModel.isPasswordVisible ? @drawable/ic_show : @drawable/ic_hide}"记得保持
endIconMode="custom",这样才会显示你绑定的自定义图标。
内容的提问来源于stack exchange,提问作者JuJu Juned
相关产品推荐
相关产品推荐

