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


当前回答

我来这篇文章寻找一种方法,有一个背景颜色的我的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>

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

其他回答

我今天在使用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"

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

如果你对ImageButton感兴趣,你可以尝试这个简单的事情:

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/ic_button"
    android:background="?attr/selectableItemBackgroundBorderless"
/>

以编程方式应用颜色:

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(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中那样改变按钮的颜色。