我使用的是Android v21支持库。

我已经创建了一个自定义背景色的按钮。当我使用背景色时,材质设计效果如波纹,显示消失了(除了点击时的抬高)。

 <Button
 style="?android:attr/buttonStyleSmall"
 android:background="?attr/colorPrimary"
 android:textColor="@color/white"
 android:textAllCaps="true"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Button1"
 />

下面是一个正常的按钮,效果很好。

<Button
 style="?android:attr/buttonStyleSmall"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:textAllCaps="true"
 android:text="Button1"
/>


当前回答

我使用了backgroundTint和前景:

<Button
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:backgroundTint="@color/colorAccent"
    android:foreground="?android:attr/selectableItemBackground"
    android:textColor="@android:color/white"
    android:textSize="10sp"/>

其他回答

我试了一下:

android:backgroundTint:"@color/mycolor"

而不是改变背景属性。 这并不能消除物质效应。

appcompat-v7的V22.1版本引入了一些新的可能性。现在可以将特定的主题仅分配给一个视图。

不赞成使用应用程序:主题样式工具栏。您现在可以使用 android:所有API级别7及以上设备的工具栏主题 android: API级别11及以上的所有小部件的主题支持 设备。

因此,我们不再在全局主题中设置所需的颜色,而是创建一个新的主题并仅将其分配给按钮

例子:

<style name="MyColorButton" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorButtonNormal">@color/myColor</item>
</style>

并使用这种风格作为主题的按钮

<Button
 style="?android:attr/buttonStyleSmall"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Button1"
 android:theme="@style/MyColorButton"/>

使用AppCompat(22.1.1+),你可以添加这样的样式:

<style name="MyGreenButton">
    <item name="colorButtonNormal">#009900</item>
</style>

并通过应用样式来使用它:

<android.support.v7.widget.AppCompatButton
    style="@style/MyGreenButton"
    android:layout_width="match_width"
    android:layout_height="wrap_content"
    android:text="A Green Button"
    />

通过编程改变颜色,我发现更新颜色的唯一方法(在API 15或16上)是使用“背景色调列表”。它不会删除API 21设备上漂亮的径向动画:

ColorStateList colorStateList = new ColorStateList(new int[][] {{0}}, new int[] {0xFF009900}); // 0xAARRGGBB
button.setSupportBackgroundTintList(colorStateList);

因为button. setbackground(…)和button. getbackground ().mutate(). setcolorfilter(…)在API 15和16中不会像在API 21中那样改变按钮的颜色。

Alex Lockwood在教程中解释了两种方法:http://www.androiddesignpatterns.com/2016/08/coloring-buttons-with-themeoverlays-background-tints.html:

方法#1:修改按钮的背景颜色w/ ThemeOverlay

<!-- res/values/themes.xml -->
<style name="RedButtonLightTheme" parent="ThemeOverlay.AppCompat.Light">
    <item name="colorAccent">@color/googred500</item>
</style>

<Button style="@style/Widget.AppCompat.Button.Colored" android:layout_width="wrap_content" android:layout_height="wrap_content" android:theme="@style/RedButtonLightTheme"/> Approach #2: Setting the AppCompatButton’s background tint <!-- res/color/btn_colored_background_tint.xml --> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Disabled state. --> <item android:state_enabled="false" android:color="?attr/colorButtonNormal" android:alpha="?android:attr/disabledAlpha"/> <!-- Enabled state. --> <item android:color="?attr/colorAccent"/> </selector> <android.support.v7.widget.AppCompatButton android:layout_width="wrap_content" android:layout_height="wrap_content" app:backgroundTint="@color/btn_colored_background_tint"/>

<android.support.v7.widget.AppCompatButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:backgroundTint="#fff"
    android:textColor="#000"
    android:text="test"/>

Use

xmlns:app="http://schemas.android.com/apk/res-auto"

颜色也会在棒棒糖前被接受