在我看来,我希望每当用户触摸布局中的 ImageView 时都会出现涟漪效应。我尝试将 ImageView 包装在 RelativeLayout 中,但没有奏效。这是布局:
<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="com.sample.me.TapActivity" >
<RelativeLayout
android:id="@+id/tap_view_frame"
android:layout_width="@dimen/activity_tap_image_width"
android:layout_height="@dimen/activity_tap_image_height"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:clickable="true"
android:background="@drawable/ripple_view" >
<ImageView
android:id="@+id/tap_view"
android:src="@drawable/ram"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:clickable="true" />
</RelativeLayout>
<TextView
android:id="@+id/tap_name_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/white"
android:layout_marginLeft="@dimen/textview_horizontal_margin"
android:layout_alignParentLeft="true"
android:layout_above="@+id/tap_number_textView"/>
<TextView
android:id="@+id/tap_number_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/white"
android:layout_marginBottom="@dimen/textview_vertical_margin"
android:layout_alignLeft="@+id/tap_name_textView"
android:layout_alignBottom="@+id/tap_view_frame" />
<android.support.v7.widget.CardView
android:id="@+id/tap_card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/cardview_horizontal_margin"
android:layout_marginRight="@dimen/cardview_horizontal_margin"
android:layout_marginTop="@dimen/cardview_vertical_margin"
android:layout_below="@+id/tap_view_frame">
<android.support.v7.widget.RecyclerView
android:id="@+id/tap_options_recyclerview"
android:layout_width="match_parent"
android:layout_height="@dimen/cardview_tap_height" />
</android.support.v7.widget.CardView>
这是“ripple_view”drawable:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#ffa0a0a0">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#ffa0a0a0"/>
</shape>
</item>
</ripple>
我也尝试添加 android:background="?android:attr/selectableItemBackground" 标记,但这似乎没有帮助。谁能解释为什么波纹没有出现?我使用相同的方法在我的自定义按钮和 RecyclerView 行中启用效果,但它不适用于 RelativeLayout。理想情况下,我想避免将第三方库用于如此简单的事情。