Android涟漪效应被选定状态覆盖 [英] Android Ripple Effect Overridden by Selected State

查看:101
本文介绍了Android涟漪效应被选定状态覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找了一段时间后,我无法找到答案...

After having been looking for a while I've not been able to find an answer to this...

我有一个回收站视图,其中的项目在被选中时具有红色背景和白色文本(之前的背景为白色,文本为黑色).为此,我使用了选择器.

I have a recycler view with items which when selected have a red background and white text (beforehand the background is white and text is black). To do this I am using a selector.

我最近尝试为此添加波纹效果,但是除非我长时间单击该项目,否则该项目的背景会直接变为红色而没有波纹.我认为这是因为选择器状态state_selected会覆盖sate_pressed上的波动吗?

I have recently tried to add a ripple effect to this, but unless I long click on the item the background of the item goes straight to red without the ripple. I am assuming this is because the selector state state_selected overrides the ripple on sate_pressed?

有人知道这是否有办法吗?这是我使用的选择器代码:

Does anyone know if there is a way around this? Here is the selector code I use:

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

    <item>
        <selector xmlns:android="http://schemas.android.com/apk/res/android" >
            <item
                android:drawable="@drawable/ripple"
                android:state_pressed="true"/>
            <item
                android:drawable="@android:color/holo_red_dark"
                android:state_selected="true"/>
            <item android:drawable="@android:color/white"/>
        </selector>
    </item>

</ripple>

提前谢谢!

推荐答案

要创建具有波纹效果并显示选定状态的选择器背景,请执行以下操作:

To create a selector background that has a ripple effect and shows selected status I do the following:

首先定义突出显示的颜色,并具有一定的透明度:

Start by defining your highlight color, with some transparency:

  • values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="selector_color">#660000ff</color>
</resources>

您可能想在棒棒糖之前使用相容性.将典型的老式选择器放入可绘制文件夹中:

You probably want to have compatibility pre-lollipop. Put a typical old-school selector inside drawable folder:

  • drawable/selector_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/selector_color" android:state_pressed="true"/>
    <item android:drawable="@color/selector_color" android:state_selected="true"/>
    <item android:drawable="@android:color/transparent"/>
</selector>

然后在drawable-v21文件夹中添加以下图层drawable:

And then add the following layer drawable inside drawable-v21 folder:

  • drawable-v21/selector_background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <selector>
            <item android:state_selected="true"
                android:drawable="@color/selector_color" />
            <item android:drawable="@android:color/transparent" />
        </selector>
    </item>
    <item>
        <ripple android:color="@color/selector_color">
            <item android:id="@android:id/mask">
                <color android:color="@android:color/white" />
            </item>
        </ripple>
    </item>
</layer-list>

现在您可以将@drawable/selector_background用作选择器.

Now you can use @drawable/selector_background for your selector.

这篇关于Android涟漪效应被选定状态覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆