在今天AppCompat更新出来之前,我可以改变Android L中按钮的颜色,但在旧版本上不行。在包含新的AppCompat更新后,我无法更改两个版本的颜色,当我尝试时,按钮就消失了。有人知道怎么改变按钮的颜色吗?
下面的图片展示了我想要达到的目标:
白色按钮是默认的,红色按钮是我想要的。
这是我之前在styles.xml中改变按钮颜色所做的:
<item name="android:colorButtonNormal">insert color here</item>
要动态地进行:
button.getBackground().setColorFilter(getResources().getColor(insert color here), PorterDuff.Mode.MULTIPLY);
我也改变了@android:style/ theme . material . light的主题父元素。到Theme.AppCompat.Light.DarkActionBar
我设置android:textColor @null在我的按钮主题,它有帮助。
styles.xml
<style name="Button.Base.Borderless" parent="Widget.AppCompat.Button.Borderless.Colored">
<item name="android:textColor">@null</item>
</style>
some_layout.xml
<Button
style="@style/Button.Base.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hint" />
现在按钮文本颜色是在AppTheme中定义的colorAccent
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="colorAccent">@color/colorAccent</item>
<item name="borderlessButtonStyle">@style/Button.Base.Borderless</item>
<item name="alertDialogTheme">@style/AlertDialog</item>
</style>
如果你想使用AppCompat样式比如widget。AppCompat. button, base。widget。AppCompat. button。彩色的,等等,你需要使用这些样式与兼容的视图从支持库。
下面的代码不适用于棒棒糖之前的设备:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/Widget.AppCompat.Button" />
你需要使用AppCompat按钮来启用AppCompat样式:
<android.support.v7.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/Widget.AppCompat.Button" />