Android布局渲染错误:找不到@attr/textAppearanceButton求助
解决方案
错误1:Failed to find '@attr/textAppearanceButton' in current theme
原因
当前应用主题未继承包含textAppearanceButton属性的Material系列主题,或自定义主题未显式定义该属性。
解决步骤
- 打开
res/values/styles.xml(或themes.xml),确保App主题继承Material组件主题,示例:<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar"> <!-- 其他主题配置 --> </style> - 若使用自定义AppCompat主题,需在主题内显式指定
textAppearanceButton对应的样式:<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="textAppearanceButton">@style/TextAppearance.MaterialComponents.Button</item> <!-- 其他主题配置 --> </style>
错误2:Style ResourceReference... is not of type STYLE (instead color)
原因
android:textAppearance属性要求传入样式资源(style),但你传入了颜色资源,类型不匹配。
解决步骤
- 仅修改文字颜色:直接使用
android:textColor替代android:textAppearance,示例:<Button android:id="@+id/register_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" android:textColor="@color/teal_200" /> - 自定义完整文字外观(含颜色、字号等):
- 在
styles.xml中创建自定义样式:<style name="CustomButtonTextStyle" parent="TextAppearance.MaterialComponents.Button"> <item name="android:textColor">@color/teal_200</item> <item name="android:textSize">16sp</item> <!-- 按需添加其他文字属性 --> </style> - 布局中引用该样式:
<Button android:id="@+id/register_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" android:textAppearance="@style/CustomButtonTextStyle" />
- 在
内容的提问来源于stack exchange,提问作者Bruh Brah
相关产品推荐
相关产品推荐

