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


当前回答

对于那些使用ImageButton的人,下面是如何做到这一点:

在style.xml:

<style name="BlueImageButton" parent="Base.Widget.AppCompat.ImageButton">
    <item name="colorButtonNormal">@color/primary</item>
    <item name="android:tint">@color/white</item>
</style>

在v21 / style.xml:

<style name="BlueImageButton" parent="Widget.AppCompat.ImageButton">
    <item name="android:colorButtonNormal">@color/primary</item>
    <item name="android:tint">@color/white</item>
</style>

然后在布局文件中:

<android.support.v7.widget.AppCompatImageButton
    android:id="@+id/my_button"
    android:theme="@style/BlueImageButton"
    android:layout_width="42dp"
    android:layout_height="42dp"
    android:layout_gravity="center_vertical"
    android:src="@drawable/ic_check_black_24dp"
    />

其他回答

一种方法是让你只指向一个样式而不是主题,你的应用程序中的所有按钮都是一样的。 在themes.xml中添加一个主题

    <style name="Theme.MyApp.Button.Primary.Blue" parent="Widget.AppCompat.Button">
        <item name="colorButtonNormal">@color/someColor</item>
        <item name="android:textColorPrimary">@android:color/white</item>
    </style>

现在在styles.xml中添加

    <style name="MyApp.Button.Primary.Blue" parent="">
        <item name="android:theme">@style/Theme.MyApp.Button.Primary.Blue</item>
    </style>

现在在你的布局中,简单地指向按钮的样式

    <Button
        ...
        style="@style/MyApp.Button.Primary.Blue"
        ...  />

与最新的支持库,你可以继承你的活动从AppCompatActivity,所以它会膨胀你的按钮作为AppCompatButton,给你一个机会,风格的每一个按钮的颜色布局与使用android:theme="@style/SomeButtonStyle",其中SomeButtonStyle是:

<style name="SomeButtonStyle" parent="@android:style/Widget.Button">
    <item name="colorButtonNormal">@color/example_color</item>
</style>

我在2.3.7,4.4.1,5.0.2中工作过

我设置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库的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"/>

如果你想用任何颜色的代码来做这个:

DrawableCompat.setTintList(button.getBackground(), ColorStateList.valueOf(yourColor));