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


当前回答

更新

使用如下所示的设计支持库(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功能集


其他回答

我设置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>

实际上,我不想改变我的自定义按钮样式,但不幸的是,它们不再工作了。

我的应用程序有一个minSdkVersion 9和一切工作之前。

我不知道为什么,但自从我删除了android:在buttonStyle之前,它似乎又工作了

现在=工作中:

<item name=“buttonStyle”>@style/ButtonmyTime</item>

之前=只是灰色材质按钮:

< name = android: buttonStyle >项目@style ButtonmyTime < - >项目

我没有特殊的文件夹为新的android版本,因为我的按钮是相当平坦的,他们应该看起来在所有android版本相同。

也许有人能告诉我为什么我要删除“机器人”: ImageButton仍在使用“android:”

<item name="android:imageButtonStyle">@style/ImageButtonmyTimeGreen</item>

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

如果你想要下面的款式

添加此样式的按钮

style="@style/Widget.AppCompat.Button.Borderless.Colored"

如果你想要这种款式

添加以下代码

style="@style/Widget.AppCompat.Button.Colored"

答案是主题,而不是风格

问题是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>

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