Android Studio中Material TextInputLayout的startIconDrawable显示灰色问题求助
解决TextInputLayout图标显示灰色的问题
这个问题我之前也碰到过!原因是Material Components的TextInputLayout默认会给起始/结束图标应用主题默认的色调(一般是灰色),所以你的彩色图标被统一“染”成灰色了,而ImageView不会自动做这个 tint 处理,所以能正常显示原色彩。
给你几个直接有效的解决方案:
方案1:在XML中禁用图标色调
直接在你的TextInputLayout标签里添加app:startIconTint="@null"属性,这样就会取消默认的色调应用,图标就能显示原本的颜色了。修改后的代码如下:
<com.google.android.material.textfield.TextInputLayout android:id="@+id/country" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="@string/country" android:layout_margin="10dp" app:startIconDrawable="@drawable/flag" app:startIconTint="@null" <!-- 新增这一行 --> app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent"> <com.google.android.material.textfield.TextInputEditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+91" android:enabled="false"/> </com.google.android.material.textfield.TextInputLayout>
方案2:在代码中动态设置
如果需要在Java/Kotlin代码里动态调整,也可以通过以下方式取消色调:
Kotlin
country.startIconTintList = null
Java
TextInputLayout country = findViewById(R.id.country); country.setStartIconTintList(null);
额外小提示
如果你的图标是VectorDrawable,还可以检查一下drawable文件里有没有设置tint属性,如果有的话也可以把它改成@null,不过上面的方案1已经足够解决问题啦。
内容的提问来源于stack exchange,提问作者Gaurav
相关产品推荐
相关产品推荐

