我使用这种样式来更改我的 Button
的背景颜色:
<style name="AccentButton" parent="Widget.AppCompat.Button.Colored">
<item name="colorButtonNormal">@color/colorAccent</item>
<item name="android:textColor">@color/white</item>
</style>
在布局中:
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/fragment_login_login_button"
app:theme="@style/AccentButton"/>
有用。但是当我在这个 Button
setEnabled(false)
时,它保持相同的颜色。我该如何处理这个案子?
原文由 Alexandr 发布,翻译遵循 CC BY-SA 4.0 许可协议
您没有正确使用
Widget.AppCompat.Button.Colored
样式。您正在使用父样式 (Widget.AppCompat.Button.Colored
),但将其用作主题。这实际上意味着Widget.AppCompat.Button.Colored
部分被完全忽略,而您只是更改按钮的默认颜色(有效,但不处理禁用的情况)。相反,您应该使用
ThemeOverlay
并分别应用Colored
样式:正如 这个关于使用
Widget.AppCompat.Button.Colored
风格的答案 中提到的,禁用颜色由colorButtonNormal
控制,启用颜色由colorAccent
控制通过使用ThemeOverlay.AppCompat.Dark
,textColor
会自动更改为深色,这意味着您可能根本不需要自定义ThemeOverlay
。