android relativelayout 点击事件,Android Relativelayout点击背景行为

本文讲述了作者在Android应用中使用RecyclerView时遇到的问题,如何为自定义布局添加可点击背景并使其在用户点击时改变颜色。同时,讨论了选项菜单背景设置对整体效果的影响,以及解决选项菜单点击时布局变色问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

所以我有一个RecyclerView,它是由我的自定义布局(代码如下)填充。我制作了一个可绘制的背景,并将其设置在我的自定义布局的根部,以便用户单击该项目时,背景颜色会发生变化。每个项目的布局上还有一个选项按钮(3个点),这也是可点击的,并且在点击时还需要更改背景颜色。Android Relativelayout点击背景行为

的问题是,当我把我作为绘制背景的选项菜单点击(下面的屏幕)

当我设置选项菜单上的背景"?android:attr/selectableItemBackground",一切正常,当整个布局不变色精细。

代码为我的布局:

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="60dp"

android:background="@drawable/item_click"

android:layout_marginBottom="2dp">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerVertical="true"

android:paddingLeft="16dp"

android:id="@+id/item_icon"

android:src="@drawable/ic_folder" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="vertical"

android:paddingLeft="72dp"

android:layout_centerVertical="true">

android:id="@+id/item_name"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textColor="#333333"

android:text="Caption"

android:textSize="13sp" />

android:id="@+id/item_description"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textColor="#333333"

android:text="Description"

android:textSize="13sp" />

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:layout_alignParentRight="true"

android:foregroundGravity="center_vertical"

android:paddingLeft="16dp"

android:paddingRight="16dp"

android:id="@+id/item_menu"

android:src="@drawable/ic_ellipsis_v"

android:background="@drawable/item_click" />

代码提拉:

android:state_focused="true"

android:state_pressed="true"

android:drawable="@color/primary_100" />

android:state_focused="false"

android:state_pressed="true"

android:drawable="@color/primary_100" />

android:drawable="@android:color/white" />

项目时的选项菜单的背景设置为"?android:attr/selectableItemBackground"选择(我想整行变色到蓝色): 19uhx.png

当选项菜单的背景设置为我的可绘制时选择的项目: 45f61608e8ac631e026709b6b1c17d4f.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值