AppCompatButton背景色API < 21的调整

38

我想在按钮上使用涟漪效果。AppCompat v22.1添加了AppCompatButton和用于AppCompat着色的新功能。

我的布局:

<android.support.v7.widget.AppCompatButton
        android:id="@+id/add_remove_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:backgroundTint="@color/primary"
        android:textColor="@android:color/white"
        android:text="Remove" />

在我的 API 22 测试设备上,涟漪效果完美运行,但我正在编写针对 API 11 的代码,不幸的是背景色需要 API >= 21。如何在旧的 API 版本上为按钮设置涟漪效果?


也许你可以在这里找到你的答案:https://dev59.com/_4Tba4cB1Zd3GeqP5WST。 - Hardy
水波纹在安卓5.0以下的版本无法直接使用,因此您需要模拟它们。 - Egor
Ripple仅适用于api>=21。 - Gabriele Mariotti
但是根据http://android-developers.blogspot.hu/2015/04/android-support-library-221.html的说法:“目前支持着色器的小部件完整列表为:[...] AppCompatButton [...]” - hunyadym
1
通过将主题应用于具有colorButtonNormal属性的按钮,我成功地为按钮着色(我尝试了4.4.4和5.1)。 - hunyadym
4个回答

110
只需使用 app:backgroundTint 替换 android:backgroundTint,着色效果将在 Lollipop 以下的版本中生效。原因是 AppCompatActivityAppCompatDelegateImplV7 使用 AppCompatViewInflater 自动将 Button 或 TextView 更改为 AppCompatButton 或 AppCompatTextView,此时 app:backgroundTint 开始生效。 图片描述

5
"app" 的定义如下:xmlns:app="http://schemas.android.com/apk/res-auto" - jk7
我有一个线性布局,其中我正在使用android:background="@drawable/background_sign_up"、app:backgroundTint="#dd282d50"和android:backgroundTintMode="multiply",该怎么办? - ashishdhiman2007
可以运行,但是会提示:“在app:backgroundTint行中发现了意外的命名空间前缀“app”用于Button标签。” - CGR
1
对于那些遇到“意外的命名空间前缀'app'”错误的人,您可能需要将Button更改为android.support.v7.widget.AppCompatButton。 - CGR
AppCompatActivity 会自动将按钮更改为 AppCompatButton,因此您可以忽略警告。 - Florian Walther
在KitKat上以编程方式实现? - user924

2

Android <21没有内置涟漪功能。这是由于性能问题:使用新API的设备可以使用RenderThread,而旧设备无法使用。请参见:http://android-developers.blogspot.de/2014/10/appcompat-v21-material-design-for-pre.html

为什么旧版Android没有涟漪效果? RippleDrawable能够平稳运行的很大一部分原因是Android 5.0的新RenderThread。为了优化旧版本Android的性能,我们暂时不支持RippleDrawable。


1
我分享一下我的使用情况:这是与ImageView有关的:
因为我在那个ImageView中使用了android:src标签作为背景图像,所以app:backgroundTint没有生效。
当我将其更改为Imageview的android:background时,app:backgroundTint就完美地工作了。
第二个用例如不同答案所述,应该使用。
androidx.appcompat.widget.AppCompatImageView

使用ImageView的替代方案。


1
为了支持API 21以下的涟漪功能,您可能需要在按钮的背景中添加一个可绘制对象:
<android.support.v7.widget.AppCompatButton
    android:id="@+id/add_remove_button"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/button_ripple"
    android:backgroundTint="@color/primary"
    android:textColor="@android:color/white"
    android:text="Remove" />

接下来,您需要在drawable和drawable-v21目录中添加同名的xml文件(如果没有,则可以创建它们,系统会自动关联)。

/res/drawable-v21/button_ripple.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/white">
    <item>
        <shape>
            <solid android:color="@color/white" />
        </shape>
    </item>
</ripple>

/res/drawable/button_ripple.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="@color/white" />
        </shape>
    </item>
</selector>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接