Ripple效果在RelativeLayout中不起作用

Var*_*mar 10 android button android-relativelayout rippledrawable

我正试图RelativeLayout在API 22上实现涟漪效应,但它没有显示出来.然而,同样的波纹起作用Button.

我的波纹drawable的代码如下:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#d1c4e9">
    <item android:id="@android:id/mask"
        android:drawable="@android:color/white" />
    <item android:drawable="@drawable/rect"/>
</ripple>
Run Code Online (Sandbox Code Playgroud)

相对布局代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@drawable/ripple">
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

在此之后,波动被设定为在背景ButtonRelativeLayout.按钮上的波纹效果很好,但根本没有出现RelativeLayout.

谁能告诉我我做错了什么?

Rag*_*dan 25

添加此属性android:clickable="true"有效.在Nexus 5上测试过


Ali*_*azi 6

除了Rahunandan所说的,如果您使用appcompat-v7支持库,您还需要添加 android:background="?attr/selectableItemBackground".


小智 5

这在布局中的属性。

android:background="?attr/selectableItemBackground"
android:clickable="true"
Run Code Online (Sandbox Code Playgroud)