我使用的是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"
/>


当前回答

我今天在使用v22库时遇到了这个问题。

假设您正在使用样式,您可以设置colorButtonNormal属性,按钮将默认使用该颜色。

<style name="AppTheme" parent="BaseTheme">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/primaryColor</item>
    <item name="colorPrimaryDark">@color/primaryColorDark</item>
    <item name="colorAccent">@color/accentColor</item>
    <item name="colorButtonNormal">@color/primaryColor</item>
</style>

除此之外,你还可以为按钮创建一个样式,然后使用每个按钮,如果你需要各种颜色(还没有测试,只是猜测)。

记住在v21样式的项目名称之前添加android:。

其他回答

我试了一下:

android:backgroundTint:"@color/mycolor"

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

我来这篇文章寻找一种方法,有一个背景颜色的我的ListView项目,但保持波纹。

我简单地添加了我的颜色作为背景,selectableItemBackground作为前景:

<style name="my_list_item">
    <item name="android:background">@color/white</item>
    <item name="android:foreground">?attr/selectableItemBackground</item>
    <item name="android:layout_height">@dimen/my_list_item_height</item>
</style>

它就像一个魔法。我想同样的技巧也可以用在按钮上。祝你好运。

下面是一种简单且向后兼容的方法,可以通过自定义背景向凸起的按钮传递涟漪效应。

你的布局应该是这样的

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/my_custom_background"
    android:foreground="?android:attr/selectableItemBackground"/>

以编程方式应用颜色:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {

    ColorStateList colorStateListRipple = new ColorStateList(
            new int[][] {{0}},
            new int[] {Color.WHITE} // ripple color
            );

    RippleDrawable rippleDrawable = (RippleDrawable) myButton.getBackground();
    rippleDrawable.setColor(colorStateListRipple);
    myButton.setBackground(rippleDrawable); // applying the ripple color
}

ColorStateList colorStateList = new ColorStateList(
        new int[][]{
                new int[]{android.R.attr.state_pressed}, // when pressed
                new int[]{android.R.attr.state_enabled}, // normal state color
                new int[]{} // normal state color
        },
        new int[]{
                Color.CYAN, // when pressed
                Color.RED, // normal state color
                Color.RED // normal state color
        }
);

ViewCompat.setBackgroundTintList(myButton, colorStateList); // applying the state colors

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"/>