Android 为什么这种连锁反应没有如预期的那样起作用?

Android 为什么这种连锁反应没有如预期的那样起作用?,android,drawable,material-design,Android,Drawable,Material Design,我正在使用drawable更改导航抽屉内文本视图的背景和文本。我希望保持文本区域的背景为白色,但通过测试,保持背景为白色不会在背景上显示涟漪效应,而是会对文本产生涟漪效应,使文本变为灰色。在下图中,中间的一个正在被按下,从而产生涟漪效应 以下是我的可绘制文件,用于更改颜色 背景: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/

我正在使用drawable更改导航抽屉内文本视图的背景和文本。我希望保持文本区域的背景为白色,但通过测试,保持背景为白色不会在背景上显示涟漪效应,而是会对文本产生涟漪效应,使文本变为灰色。在下图中,中间的一个正在被按下,从而产生涟漪效应

以下是我的可绘制文件,用于更改颜色

背景:

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

正文:


文本视图布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25.0sp"
        android:background="@drawable/activated_background"
        android:textColor="@drawable/activated_text"
        android:id="@id/text1"
        android:layout_weight="0"
        android:layout_marginTop="8dip"
        android:layout_marginBottom="8dip" />

</RelativeLayout>


如果使用自定义背景,则不会显示涟漪效果。您可能需要使用其他ripple库,例如。

您应该使用2个可绘制文件,并将其用作视图背景。 对于lolipop之前的版本:

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

</selector>

对于lolipop(v21):


在我的例子中,涟漪效应在第一次点击后起作用,但对于第一次点击,它对我不起作用。已使用android:state_activated=“true”和main.xmlandroid:clickable=“true”更改了背景选择器文件,则该文件可以一直正常工作

selector.xml(在res\drawable\selector.xml下)


在activity_main.xml中

 <com.mysample.RecyclingImageView
    android:id="@+id/imageview_overlay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/selector"
    android:clickable="true" />


不过,在灰色背景上,涟漪效果看起来非常完美。我想融入背景(白色)的那些看起来不太好。我把它添加到了问题中。我知道要为棒棒糖前的支持创建另一个,我首先关注棒棒糖后的设计问题。尝试使用棒棒糖一个失败的一点,选定的一个没有改变颜色。我每次尝试更改所选内容的颜色都失败了。下面的图片显示了这是一种解决方案:首先,蓝色文本中的所选内容没有所选背景,在图片中单击中间的一个,选定的背景不会填充其他项目的填充空间。请参阅下面的我的答案
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/white_list_item_selected_background" >
    <item android:drawable="@android:color/white" />

</ripple>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@drawable/card_bg_pressed" android:state_enabled="true" android:state_pressed="true"/>
<item android:state_activated="true" android:drawable="@drawable/card_bg_focused" android:state_enabled="true" android:state_focused="true"/>
<item android:state_activated="true" android:drawable="@drawable/card_bg_selected" android:state_enabled="false" android:state_selected="true"/>
</selector>
 <com.mysample.RecyclingImageView
    android:id="@+id/imageview_overlay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/selector"
    android:clickable="true" />