我看到了一些问题,他们给出了一些可能的方法来实现我想要的.例如:

  1. 在样式中使用colorControlHighlight属性.xml.

    这是我的风格——v21.xml:

    <style name="SelectableItemBackground">
        <item name="android:colorControlHighlight">#5677FC</item>
        <item name="android:background">?attr/selectableItemBackground</item>
    </style>
    

    和我的小工具:

    <TextView
        android:id="@+id/tv_take_photo_as_bt"
        android:layout_width="280dp"
        android:layout_height="48dp"
        android:text="@string/act_take_photo"
        style="@style/SelectableItemBackground"/>
    

    但它不起作用.我还try 将parent="Theme.AppCompat添加到"SelectableItemBackground"样式中,或者更改为colorControlHighlight(no android: prefix)",或者更改为?android:attr/selectableItemBackground,这两种样式都没有用.

  2. 在布局中使用backgroundTint属性.

    所以我在TextView的基础上加了android:backgroundTint="#5677FC".还是没用.然后我试着把android:backgroundTintMode改成src_insrc_atop,结果都没什么不同.

那么,当我使用?attr/selectableItemBackground作为背景时,我如何才能改变波纹 colored颜色 呢?我只关注棒棒糖及以上.提前谢谢您!

推荐答案

最后我找到了解决办法:我应该写另一种风格,而不是在主题SelectableItemBackground中直接使用android:colorControlHighlight:

<style name="SelectableItemTheme">
    <item name="colorControlHighlight">@color/ripple_color</item>
</style>

然后:

<style name="SelectableItemBackground">
    <item name="android:theme">@style/SelectableItemTheme</item>
    <item name="android:background">?attr/selectableItemBackground</item>
</style>

最后在布局中添加style="@style/SelectableItemBackground"View.xml.

UPDATED ON 2016/8/26

我想展示一个连锁react ,当用户touch /点击API21上CardView的时候,当然在棒棒糖之前应该有另一种反馈.所以我应该写:

<android.support.v7.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:foreground="@drawable/selectable_item_background"/>

drawable文件夹中有selectable_item_background个:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false" android:drawable="@android:color/transparent" />
    <item android:drawable="@color/color_clicked" />
</selector>

101文件夹中的selectable_item_background:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ripple_black" />
</selector>

最后,ripple_black in drawable(或drawable-v21)文件夹:

<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:color="@color/color_clicked"
    tools:ignore="NewApi" /> <!--you can remove this line if it's in v21 folder-->

就这样.对于其他视图,可能应该使用android:background="@drawable/selectable_item_background".别忘了为他们设定OnClickListenerOnTouchListener或类似的数字,否则ripple不会出现.

Android相关问答推荐

Jetpack编写日期 Select 器

有没有办法知道每台安卓设备上的通知限制?

Android应用程序中的背景问题

如何在Jetpack composeH中创建具有弯曲末端的六边形形状

Android从已连接的设备获得GATT

OverridePendingTransition已弃用,我该怎么办?

Jetpack Compose:带芯片的Textfield

Dispatchers中的Kotlin协同程序.Main没有';t块主螺纹

升级到 Jetpack Compose 物料 list 2023.08.00 需要我将 targetSdk 更改为 34

从包含的(复合)模块导入 Kotlin 类时,Android 应用程序模块的 build.gradle.kts 未解决的引用错误

如何在另一个函数中初始化主类参数? (我是初学者)代码推荐

如何在jetpack compose中使可组合的屏幕zoom 到不同的手机(屏幕)尺寸?

Android 构建失败:找不到 flexbox2.0.1.aar

将 CircularProgressIndicator 添加到按钮而不增加其高度

设置背景图片组成Column

Android 设备断开连接后发送的 BLE 蓝牙数据

安卓模拟器打不开

如何在 Jetpack Compose 中设置行宽等于 TextField 的宽度?

在 Jetpack Compose 中找不到 Path 的减号()

Jetpack compose 绘制形状