Android 带有背景色的按钮上的材质效果

Android 带有背景色的按钮上的材质效果,android,android-5.0-lollipop,Android,Android 5.0 Lollipop,我正在使用Android v21支持库 我已经创建了一个自定义背景颜色的按钮。当我使用背景色时,诸如涟漪、分隔缝等材质设计效果将消失(单击时的高程除外) <Button style="?android:attr/buttonStyleSmall" android:background="?attr/colorPrimary" android:textColor="@color/white" android:textAllCaps="true" android:layout_wi

我正在使用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:background时,您正在用空白颜色替换按钮的大部分样式和外观

更新:自的起,有一种新样式将主题的
colorButtonNormal
用于禁用的颜色,将
colorAccent
用于启用的颜色

这允许您通过直接将其应用于按钮

<Button
  ...
  style="@style/Widget.AppCompat.Button.Colored" />
这将确保您的背景色为
?attr/colorPrimary
,并具有使用默认
?attr/colorControlHighlight
的默认涟漪动画(如果您愿意,也可以在主题中设置)

注意:您必须为v21以下版本创建自定义选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/primaryPressed" android:state_pressed="true"/>
    <item android:drawable="@color/primaryFocused" android:state_focused="true"/>
    <item android:drawable="@color/primary"/>
</selector>


假设您有一些默认、按下和聚焦状态所需的颜色。就我个人而言,我在被选中的中途拍摄了一张涟漪的屏幕截图,并将主/焦点状态从中拉出来。

还有另一个简单的解决方案,可以为“平面”按钮提供自定义背景,同时保持其“材质”效果

  • 将您的按钮放置在具有所需背景设置的ViewGroup中
  • 将当前主题设置为按钮的背景(API>=11)
  • i、 e:

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/blue">
        <Button
            style="?android:attr/buttonStyleSmall"
            android:background="?android:attr/selectableItemBackground"
            android:textColor="@android:color/white"
            android:textAllCaps="true"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Button1"
            />
    </FrameLayout>
    
    
    
    可以用于按钮,它在API>=11上工作,并且您将在>=21设备上获得涟漪效果,在AppCompat更新以支持ripple之前,将常规按钮保留在pre-21上


    您也可以仅用于>=21个按钮。

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

    使用AppCompat(22.1.1+)可以添加如下样式:

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

    因为
    button.setBackground(…)
    button.getBackground().mutate().setColorFilter(…)
    不会像在API 21上那样更改API 15和16上的按钮颜色。

    如果您可以使用第三方库,请退出。它允许您只需几行代码就可以向任何视图添加连锁反应。您只需要在xml布局文件中,用
    com.andexert.library.RippleView
    容器包装想要产生连锁反应的元素

    作为一个额外的好处,它需要Min SDK 9,这样您就可以跨操作系统版本实现设计一致性

    以下是一个取自图书馆GitHub repo的示例:

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

    使用
    backgroundTint
    而不是
    background
    以下是一种简单且向后兼容的方法,可将涟漪效果传递给具有自定义背景的凸起按钮

    您的布局应该如下所示

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/my_custom_background"
        android:foreground="?android:attr/selectableItemBackground"/>
    
    
    
    我尝试了以下方法:

    android:backgroundTint:"@color/mycolor"
    
    而不是更改背景属性。
    这并没有消除实质性影响。
    appcompat-v7
    v22.1
    引入了一些新的可能性。现在可以只为一个视图指定特定主题

    不推荐使用app:theme设置工具栏样式。您现在可以使用 android:所有API级别7及更高设备上的工具栏主题,以及 android:对API级别11及更高的所有小部件的主题支持 设备

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

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

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

    @ianhanniballake的答案绝对正确且简单。但我花了几天时间才理解。对于不理解其答案的人,这里有更详细的实现

    <Button
            android:id="@+id/btn"
            style="@style/MaterialButton"
            ... />
    
    
    <style name="MaterialButton" parent="Widget.AppCompat.Button.Colored">
        <item name="android:theme">@style/Theme.MaterialButton</item>
       ...
    </style>
    
    
    <style name="Theme.MaterialButton" parent="YourTheme">
        <item name="colorAccent">@color/yourAccentColor</item>
        <item name="colorButtonNormal">@color/yourButtonNormalColor</item>
    </style>
    
    
    @style/Theme.MaterialButton
    ...
    @颜色/颜色
    @颜色/按钮正常颜色
    
    ==或===

    <Button
            android:id="@+id/btn"
            style="@style/Widget.AppCompat.Button.Colored"
            android:theme="@style/Theme.MaterialButton" />
    
    <style name="Theme.MaterialButton" parent="YourTheme">
        <item name="colorAccent">@color/yourAccentColor</item>
        <item name="colorButtonNormal">@color/yourButtonNormalColor</item>
    </style>
    
    
    @颜色/颜色
    @颜色/按钮正常颜色
    

    在lolipop之前,颜色会变浅

    我使用了背景色和前景色:

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

    我来这篇文章是想找到一种方法,既能为我的
    列表视图
    项目添加背景色,又能保持波纹

    我只是将我的颜色添加为背景,将selectableItemBackground添加为前景:

    <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"/>
    
    <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>
    
    
    @颜色/白色
    ?属性/可选择项背景
    @尺寸/我的清单项目高度
    

    它就像一个符咒。我想同样的技术也可以用于按钮。祝你好运:)

    如果你对ImageButton感兴趣,你可以试试这个简单的东西:

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

    伟大的教程be Alex Lockwood中介绍了两种方法:

    方法#1:修改按钮的背景色,同时显示主题

    <!-- res/values/themes.xml -->
    <style name="RedButtonLightTheme" parent="ThemeOverlay.AppCompat.Light">
        <item name="colorAccent">@color/googred500</item>
    </style>
    
    
    @颜色/灰色500
    
    
    
    方法#2:设置AppCompatButton的背景色调

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

    以编程方式应用颜色:

    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
    

    出现这种情况是因为你用纯色替换了所有漂亮的材质效果的背景。至于如何在保持材质效果的同时正确地给按钮上色,目前还没有人能弄清楚这一点。@SweetWisher the ri
    <!-- 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"/>
    
    <!-- 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"/>
    
    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