在今天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


当前回答

如果你想使用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" />

其他回答

我刚刚创建了一个android库,它允许您轻松地修改按钮颜色和波纹颜色

https://github.com/xgc1986/RippleButton

<com.xgc1986.ripplebutton.widget.RippleButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btn"
    android:text="Android button modified in layout"
    android:textColor="@android:color/white"
    app:buttonColor="@android:color/black"
    app:rippleColor="@android:color/white"/>

你不需要为每个你想要不同颜色的按钮创建一个样式,允许你随机自定义颜色

这在AppCompat库的v23.0.0中得到了增强 随着更多主题的加入,包括

Widget.AppCompat.Button.Colored

首先,包括appCompat依赖关系(如果还没有的话)

compile('com.android.support:appcompat-v7:23.0.0') {
    exclude group: 'com.google.android', module: 'support-v4'
}

现在因为你需要使用compat的v23版本,你也需要针对SDK-v23 !

    compileSdkVersion = 23
    targetSdkVersion = 23

在你的价值观/主题中

<item name="android:buttonStyle">@style/BrandButtonStyle</item>

在你的价值观/风格中

<style name="BrandButtonStyle" parent="Widget.AppCompat.Button.Colored">
    <item name="colorButtonNormal">@color/yourButtonColor</item>
    <item name="android:textColor">@color/White</item>
</style>

在你的values-v21/样式中

<style name="BrandButtonStyle" parent="Widget.AppCompat.Button.Colored">
    <item name="android:colorButtonNormal">@color/yourButtonColor</item>
    <item name="android:textColor">@color/White</item>
</style>

因为你的按钮主题是基于Widget.AppCompat.Button.Colored按钮上的文本颜色默认是白色!

但似乎有一个问题,当你禁用按钮,按钮将改变其颜色为浅灰色,但文本颜色将保持白色!

解决这个问题的方法是专门将按钮上的文本颜色设置为白色! 正如我在上面所示的风格所做的那样。

现在你可以简单地定义你的按钮,让AppCompat做剩下的:)

<Button
        android:layout_width="200dp"
        android:layout_height="48dp" />

禁用状态

启用状态

编辑:

添加<Button android:theme="@style/BrandButtonStyle"/>

在支持库rev.22中正式修复(2015年3月13日星期五)。参见相关谷歌代码问题:

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

使用的例子

theme.xml:

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

v21 / theme.xml

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

在我的情况下,而不是使用按钮,我使用androidx. appcompat.com widget. appcompatbutton 这对我很有效。

布局:

<android.support.v7.widget.AppCompatButton
  style="@style/MyButton"
  ...
  />

styles.xml:

<style name="MyButton" parent="Widget.AppCompat.Button.Colored">
  <item name="backgroundTint">@color/button_background_selector</item>
  <item name="android:textColor">@color/button_text_selector</item>
</style>

颜色/ button_background_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="#555555"/>
    <item android:color="#00ff00"/>
</selector>

颜色/ button_text_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="#888888"/>
    <item android:color="#ffffff"/>
</selector>