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


当前回答

答案是主题,而不是风格

问题是Button的颜色与主题的colorButtonNormal一致。我尝试过很多不同的方法来改变风格,但都没有成功。所以我改变了按钮的主题。

用colorButtonNormal和colorPrimary创建一个主题:

<style name="ThemeAwesomeButtonColor" parent="AppTheme">
    <item name="colorPrimary">@color/awesomePrimaryColor</item>
    <item name="colorButtonNormal">@color/awesomeButtonColor</item>
</style>

在按钮中使用此主题

<Button
        android:id="@+id/btn_awesome"
        style="@style/AppTheme.Button"
        android:theme="@style/ThemeAwesomeButtonColor"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_awesome"/>

“AppTheme。按钮”可以是任何东西扩展按钮样式,就像这里我使用原色作为文本颜色:

<style name="AppTheme.Button" parent="Base.Widget.AppCompat.Button">
    ...
    <item name="android:textColor">?attr/colorPrimary</item>
    ...
</style>

你可以得到你想要的任何颜色的按钮,与材料设计兼容。

其他回答

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

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

更改单个按钮的颜色

ViewCompat.setBackgroundTintList(button, getResources().getColorStateList(R.color.colorId));

一种方法是让你只指向一个样式而不是主题,你的应用程序中的所有按钮都是一样的。 在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"
        ...  />

更新

使用如下所示的设计支持库(23.2.0)和应用程序兼容性小部件

在Android支持库22.1中:

这是在膨胀布局-替换按钮时自动完成的 与appcompatextview, TextView与AppCompatTextView等确保 每个都可以支持着色。在这个版本中,那些色调意识 widget现在是公开可用的,允许您继续着色 支持,即使您需要子类化一个受支持的小部件。

色彩感知小部件的完整列表:

AppCompatAutoCompleteTextView
AppCompatButton
AppCompatCheckBox
AppCompatCheckedTextView
AppCompatEditText
AppCompatMultiAutoCompleteTextView
AppCompatRadioButton
AppCompatRatingBar
AppCompatSpinner
AppCompatTextView

前棒棒糖装置的材料设计:

AppCompat(又名ActionBarCompat)最初是作为 Android 4.0 ActionBar API用于运行在姜饼上的设备, 在反向移植实现的基础上提供一个公共API层 以及框架实现。AppCompat v21提供了一个API和 最新的Android 5.0功能集


对于那些使用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"
    />