Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在Imageview上设置selectableItemBackground?_Android_Xml_Android Layout - Fatal编程技术网

Android 如何在Imageview上设置selectableItemBackground?

Android 如何在Imageview上设置selectableItemBackground?,android,xml,android-layout,Android,Xml,Android Layout,我有一个RecyclerView,每个项目都在项目布局上启用了“selectableItemBackground”,让我在触摸时产生连锁反应。问题是,项目上有一个imageView,涟漪效应只发生在图像下。如何让涟漪出现在完整的项目布局上(也出现在imageView上),就像在Google Playstore上触摸项目一样?或者您可以尝试以下方法:- 只需使用cardwiew <android.support.v7.widget.CardView xmlns:app="ht

我有一个RecyclerView,每个项目都在项目布局上启用了“selectableItemBackground”,让我在触摸时产生连锁反应。问题是,项目上有一个imageView,涟漪效应只发生在图像下。如何让涟漪出现在完整的项目布局上(也出现在imageView上),就像在Google Playstore上触摸项目一样?

或者您可以尝试以下方法:-
只需使用
cardwiew

<android.support.v7.widget.CardView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:clickable="true"
        android:focusable="true"
        app:cardCornerRadius="4dp"
        android:foreground="?android:attr/selectableItemBackground">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v7.widget.CardView>

尝试在XML文件中的RecyclerView中使用android:drawSelectorOnTop=“true”。 并将Ripple effect selector设置为您的RecyclerView单元格的android:background属性

drawable-v21文件夹中单元格的选择器示例:

ripple_selector.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/preference_item_pressed"> <!-- ripple color -->
    <item android:id="@android:id/mask">
        <color android:color="@android:color/white" />
    </item> <!-- normal color -->
</ripple>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="72dp" android:layout_height="72dp"
    android:background="@drawable/ripple_selector">
</RelativeLayout>

以及具有选择器背景的单元格示例:

cell.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/preference_item_pressed"> <!-- ripple color -->
    <item android:id="@android:id/mask">
        <color android:color="@android:color/white" />
    </item> <!-- normal color -->
</ripple>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="72dp" android:layout_height="72dp"
    android:background="@drawable/ripple_selector">
</RelativeLayout>

我在
前台
便利值
?android:attr/selectableItemBackground“
中解决了父内容中的设置,并启用了焦点

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:foreground="?android:attr/selectableItemBackground"
        android:focusable="true">

        <ImageView
            android:id="@+id/cover_image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:contentDescription="@string/desc_empty"
            android:scaleType="centerCrop"
            android:src="@drawable/default_image" />

  </RelativeLayout>

视图
插入到其他
视图组
会影响应用程序性能,因此您只需将
图像视图
更改为
图像按钮
,并将其作为背景

<androidx.appcompat.widget.AppCompatImageButton
    android:id="@+id/myButton"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="?attr/selectableItemBackground"
    android:scaleType="centerCrop"
    app:srcCompat="@drawable/ic_arrow_white" />


它仍然位于单元格的图像视图下。您是否添加了
android:clickable=“true”android:focusable=“true”“
到您的父布局?这实际上很好。但它只适用于api 21及以上版本是吗?是的。只能使用api 21中的Ripple effect。但是你可以使用一些库,让你在棒棒糖前的版本中产生涟漪。试试我试过了,selectableItemBackground在触摸屏上不起作用。基本上什么也没发生。我已经编辑了代码:)将
android:clickable=“true”
添加到
cardwiew
。这很有效。除非您需要将焦点设置为true。@SeargantPeauts至少接受答案。@peshkira只有在您的父项是FrameLayout时才有效,因为api<23,
前台在23之前的视图中不存在,不要相信您应该将相同的文档应用于项目布局作为背景。你可以分享你的布局。很好,使用前景,你甚至可以添加不同的背景,“按下”效果仍然存在!谢谢!