我使用这种样式来更改我的背景颜色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"/>
有用。但是,当我叫setEnabled(false)
这个Button
,它保持相同的颜色。如何处理此案?
您没有Widget.AppCompat.Button.Colored
正确使用样式。您使用的是父样式(Widget.AppCompat.Button.Colored
),但将其用作主题。这实际上意味着该Widget.AppCompat.Button.Colored
零件将被完全忽略,而您只是在更改按钮的默认颜色(该按钮有效,但不能处理禁用的情况)。
相反,您应该使用ThemeOverlay
和分别应用Colored
样式:
<style name="AccentButton" parent="ThemeOverlay.AppCompat.Dark">
<!-- customize colorButtonNormal for the disable color -->
<!-- customize colorAccent for the enabled color -->
</style>
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/fragment_login_login_button"
android:theme="@style/AccentButton"
style="@style/Widget.AppCompat.Button.Colored"/>
如该答案中Widget.AppCompat.Button.Colored
提到的使用样式一样,禁用的颜色由控制,colorButtonNormal
启用的颜色由控制colorAccent
。通过使用ThemeOverlay.AppCompat.Dark
,textColor
会自动更改为深色,这意味着您可能根本不需要该自定义ThemeOverlay
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句