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


当前回答

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

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

其他回答

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

你的布局应该是这样的

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

使用backgroundTint代替background

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

如果您可以使用第三方库,请查看traex/RippleEffect。它允许您仅用几行代码就可以向任何视图添加涟漪效应。你只需要在你的xml布局文件中,包装你想用com. anddexert .library. rippleview容器产生涟漪效应的元素。

作为额外的奖励,它需要Min SDK 9,这样你就可以在不同的操作系统版本之间保持设计一致性。

下面是一个摘自图书馆GitHub回购的例子:

<com.andexert.library.RippleView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:rv_centered="true">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@android:drawable/ic_menu_edit"
        android:background="@android:color/holo_blue_dark"/> 

</com.andexert.library.RippleView>

你可以通过在RippleView元素中添加这个属性来改变波纹颜色:app:rv_color="@color/ my_fany_ripple_color "

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