Android:Material TextInputLayout的app:boxStrokeColor属性为何不生效?
解决TextInputLayout边框颜色不生效的建议
- 确认使用的是Material Components库的
TextInputLayout,完整类名为com.google.android.material.textfield.TextInputLayout,而非AppCompat版本的组件。 - 确保
boxStrokeColor使用ColorStateList定义不同状态下的颜色,而非单一纯色。示例:
在布局文件中引用颜色状态列表:
在<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:boxStrokeColor="@color/text_input_stroke_color" style="@style/Widget.MaterialComponents.TextInputLayout.OutlineBox"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content"/> </com.google.android.material.textfield.TextInputLayout>res/color/text_input_stroke_color.xml中定义状态颜色:<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#FF4081" android:state_focused="true"/> <item android:color="#F44336" android:state_error="true"/> <item android:color="#9E9E9E"/> <!-- 默认未聚焦状态 --> </selector> - 检查TextInputLayout的样式:仅OutlineBox或FilledBox样式支持
boxStrokeColor属性,需在布局中指定对应样式,如上述示例中的@style/Widget.MaterialComponents.TextInputLayout.OutlineBox。 - 排查主题全局覆盖:检查
styles.xml中的AppTheme,是否通过textInputStyle全局设置了boxStrokeColor,导致局部配置被覆盖。 - 确认代码未动态覆盖:检查是否在Java/Kotlin代码中调用
setBoxStrokeColor()等方法,覆盖了XML中的属性设置。
内容的提问来源于stack exchange,提问作者deja
相关产品推荐
相关产品推荐

