问题
I am trying to make all buttons in my app have default color using colorButtonNormal
in my style.
It works nice on API 21 and above but under API 21 it only changes the background of some buttons and i dont know what is going wrong.
styles.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowContentOverlay">@null</item>
<item name="android:textColorPrimary">@color/white</item>
<item name="colorPrimary">@color/btn_login</item>
<item name="colorPrimaryDark">@color/bg_login</item>
<item name="colorAccent">@color/btn_login</item>
<item name="colorButtonNormal">@color/btn_login</item>
</style>
</resources>
v21/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowContentOverlay">@null</item>
<item name="android:textColorPrimary">@color/white</item>
<item name="android:alertDialogTheme">@style/AlertDialogCustom</item>
<item name="android:colorButtonNormal">@color/btn_login</item>
<item name="colorPrimary">@color/btn_login</item>
<item name="colorPrimaryDark">@color/bg_login</item>
<item name="colorAccent">@color/btn_login</item>
</style>
<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:colorPrimary">@color/btn_login</item>
<item name="android:colorAccent">@color/btn_login</item>
<item name="colorAccent">@color/btn_login</item>
<item name="colorPrimary">@color/btn_login</item>
<item name="colorPrimaryDark">@color/bg_login</item>
</style>
<style name="Preference" parent="Theme.AppCompat.Light">
<item name="android:textColorPrimary">@color/black</item>
<item name="android:colorPrimary">@color/btn_login</item>
<item name="android:colorAccent">@color/btn_login</item>
<item name="android:editTextColor">@color/black</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:alertDialogTheme">@style/AlertDialogCustom</item>
<item name="colorAccent">@color/btn_login</item>
</style>
<style name="EditTextThemeCustom" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="android:editTextColor">@color/black</item>
<item name="android:textColor">@color/black</item>
<item name="colorAccent">@color/btn_login</item>
</style>
</resources>
Result:
Lollipop
and
Kitkat
Any suggestions?
回答1:
Add to your styles.xml
<style name="ColoredButton" parent="Widget.AppCompat.Button">
<item name="colorButtonNormal">@color/btn_login</item>
</style>
and then use
android:theme="@style/ColoredButton"
as one of your buttons' attributes
回答2:
The buttons you inflate will get automatically translated to AppCompatButton
.
Wherever you have new Button(context)
you need to use new AppCompatButton(context)
instead for Material theme colors to apply.
回答3:
I was getting this issue in old devices(<21) because, my activity was extending just activity when I make in it extend AppCompatActivity, in old devices also it is working perfect.
来源:https://stackoverflow.com/questions/33305027/cant-change-colorbuttonnormal-value-to-all-buttons