Android ViewGroup点击效果(背景色)

本文介绍在开发Android应用时如何通过使用图片资源和颜色来实现ViewGroup的点击效果,包括定义drawable资源、设置背景以及使布局可点击。提供两种简单实现方法,帮助开发者提升用户体验。

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

在开发Android应用的界面时,我们必然会用到本文ViewGroup,尤其是FrameLayout,LinearLayout,RelativeLayout等ViewGroup的子类; 在一些情况下,我们需要设置这些ViewGroup的点击效果,使用户获得更好的体验。下面介绍两种实现方法:

方法一:使用图片资源

通过为ViewGroup设置不同的图片图片资源,是最方便的实现方法,我们只需要设计两张图片,一张为非点击效果,另一张为点击时效果,然后为ViewGroup设置 

background即可:

1. 定义ViewGroup的background所需的drawbale资源:selector_viewgroup_item_btn_bg.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <item android:drawable="@drawable/btn_bg_pressed" android:state_pressed="true"/>  
  5.     <item android:drawable="@drawable/btn_bg_unpressed"/>  
  6.   
  7. </selector>  

该文件的定义很简单,就是规定了一个点击效果图片和一个正常情况下的图片,通过state进行区分。


2.  定义布局文件:main_activity.xml

  1. <RelativeLayout  
  2.         android:id="@+id/my_collect_layout_parent"  
  3.         android:layout_width="fill_parent"  
  4.         android:layout_height="50dp"  
  5.         android:clickable="true"  
  6.         android:background="@drawable/selector_viewgroup_item_btn_bg" >  
  7.   
  8.         <ImageView  
  9.             android:id="@+id/img1"  
  10.             android:layout_width="wrap_content"  
  11.             android:layout_height="wrap_content"  
  12.             android:layout_alignParentLeft="true"  
  13.             android:layout_centerVertical="true"  
  14.             android:layout_marginLeft="20dp"  
  15.             android:src="@drawable/more_my_collect_img" />  
  16.   
  17.         <TextView  
  18.             android:id="@+id/my_collect_text"  
  19.             android:layout_width="wrap_content"  
  20.             android:layout_height="wrap_content"  
  21.             android:layout_centerVertical="true"  
  22.             android:layout_marginLeft="12dp"  
  23.             android:layout_toRightOf="@id/img1"  
  24.             android:gravity="center"  
  25.             android:text="我的收藏"  
  26.             android:textColor="#5d5d5d"  
  27.             android:textSize="15sp" />  
  28.     </RelativeLayout>  

在布局文件中,我们有两处处需要 注意:

  ·设置RelativeLayout的background属性,指向之前定义的drawable资源selector_viewgroup_item_btn_bg.xml

  ·要为RelativeLayout设置clickable 属性: android:clickable="true"

效果图:




方法二: 使用Color颜色

1. 在value目录下定义drawables.xml文件:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <resources>  
  3.     <!-- 用于RelativeLayout点击 -->  
  4.     <drawable name="viewgroup_item_bg_unpress">#ffffff</drawable>    
  5.     <drawable name="viewgroup_item_bg_pressed">#f2f2f2</drawable>    
  6. </resources>  

注: 此处我们需要注意,item的开头我们使用的是<drawable>而不是<color>.


2. 定义ViewGroup的background所需的drawbale资源:selector_viewgroup_bg.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <item android:state_pressed="true" android:drawable="@drawable/viewgroup_item_bg_pressed"/>  
  5.     <item android:drawable="@drawable/viewgroup_item_bg_unpress" android:state_focused="false" android:state_pressed="false"/>  
  6. </selector>  


3. 定义布局文件:main_activity.xml

  1. <RelativeLayout  
  2.         android:layout_width="fill_parent"  
  3.         android:layout_height="50dp"  
  4.         android:clickable="true"  
  5.         android:background="@drawable/selector_viewgroup_bg" >  
  6.   
  7.         <ImageView  
  8.             android:id="@+id/img1"  
  9.             android:layout_width="wrap_content"  
  10.             android:layout_height="wrap_content"  
  11.             android:layout_alignParentLeft="true"  
  12.             android:layout_centerVertical="true"  
  13.             android:layout_marginLeft="20dp"  
  14.             android:src="@drawable/more_my_collect_img" />  
  15.   
  16.         <TextView  
  17.             android:id="@+id/my_collect_text"  
  18.             android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content"  
  20.             android:layout_centerVertical="true"  
  21.             android:layout_marginLeft="12dp"  
  22.             android:layout_toRightOf="@id/img1"  
  23.             android:gravity="center"  
  24.             android:text="我的收藏"  
  25.             android:textColor="#5d5d5d"  
  26.             android:textSize="15sp" />  
  27.     </RelativeLayout>  

在布局文件中,我们有两处处需要 注意:

  ·设置RelativeLayout的background属性,指向之前定义的drawable资源selector_viewgroup_bg.xml

  ·要为RelativeLayout设置clickable 属性: android:clickable="true"

效果图:



通过上述方法,即可实现最简单的ViewGroup点击效果。


源代码下载地址(免费):http://download.youkuaiyun.com/detail/zuiwuyuan/8401989

转载自 http://blog.youkuaiyun.com/zuiwuyuan/article/details/43199625

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值