使用 Material Design 和 AppCompat 为 Android 中的按钮着色 [英] Coloring Buttons in Android with Material Design and AppCompat

查看:22
本文介绍了使用 Material Design 和 AppCompat 为 Android 中的按钮着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在今天 AppCompat 更新发布之前,我可以更改 Android L 中按钮的颜色,但不能更改旧版本.在包含新的 AppCompat 更新后,我无法更改任一版本的颜色,当我尝试时该按钮就会消失.有谁知道如何更改按钮颜色?

Before the AppCompat update came out today I was able to change the color of buttons in Android L but not on older versions. After including the new AppCompat update I am unable to change the color for either version, when I do try the button just disappears. Does anyone know how to change the button color?

下图展示了我想要实现的目标:

The following pictures shows what I want to achieve:

白色按钮是默认的,红色是我想要的.

The white button is default, the red one is what I want.

这是我之前在 styles.xml 中更改按钮颜色时所做的:

This is what I was doing previously to change the color of the buttons in the 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.DarkActionBar 更改为 Theme.AppCompat.Light.DarkActionBar

Also I did change the theme parent from @android:style/Theme.Material.Light.DarkActionBar to Theme.AppCompat.Light.DarkActionBar

推荐答案

在 Support Library rev.22(2015 年 3 月 13 日星期五)中正式修复.查看相关的谷歌代码问题:

Officially fixed in Support Library rev.22 (Fri March 13, 2015). See relevant google code issue:

https://issuetracker.google.com/issues/37008632

使用示例

theme.xml:

<item name="colorButtonNormal">@color/button_color</item>

v21/theme.xml

v21/theme.xml

<item name="android:colorButtonNormal">@color/button_color</item>

这篇关于使用 Material Design 和 AppCompat 为 Android 中的按钮着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆