Android ViewGroup点击效果(背景色)
在開發(fā)Android應用的界面時,我們必然會用到本文ViewGroup,尤其是FrameLayout,LinearLayout,RelativeLayout等ViewGroup的子類; 在一些情況下,我們需要設置這些ViewGroup的點擊效果,使用戶獲得更好的體驗。下面介紹兩種實現(xiàn)方法:
方法一:使用圖片資源
通過為ViewGroup設置不同的圖片圖片資源,是最方便的實現(xiàn)方法,我們只需要設計兩張圖片,一張為非點擊效果,另一張為點擊時效果,然后為ViewGroup設置?
background即可:
1. 定義ViewGroup的background所需的drawbale資源:selector_viewgroup_item_btn_bg.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/btn_bg_pressed" android:state_pressed="true"/><item android:drawable="@drawable/btn_bg_unpressed"/></selector>該文件的定義很簡單,就是規(guī)定了一個點擊效果圖片和一個正常情況下的圖片,通過state進行區(qū)分。
2. ?定義布局文件:main_activity.xml
<RelativeLayoutandroid:id="@+id/my_collect_layout_parent"android:layout_width="fill_parent"android:layout_height="50dp"android:clickable="true"android:background="@drawable/selector_viewgroup_item_btn_bg" ><ImageViewandroid:id="@+id/img1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_centerVertical="true"android:layout_marginLeft="20dp"android:src="@drawable/more_my_collect_img" /><TextViewandroid:id="@+id/my_collect_text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_marginLeft="12dp"android:layout_toRightOf="@id/img1"android:gravity="center"android:text="我的收藏"android:textColor="#5d5d5d"android:textSize="15sp" /></RelativeLayout>在布局文件中,我們有兩處處需要 注意:
? ·設置RelativeLayout的background屬性,指向之前定義的drawable資源selector_viewgroup_item_btn_bg.xml
? ·要為RelativeLayout設置clickable 屬性:?android:clickable="true"
效果圖:
方法二: 使用Color顏色
1. 在value目錄下定義drawables.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <resources><!-- 用于RelativeLayout點擊 --><drawable name="viewgroup_item_bg_unpress">#ffffff</drawable> <drawable name="viewgroup_item_bg_pressed">#f2f2f2</drawable> </resources>
注: 此處我們需要注意,item的開頭我們使用的是<drawable>而不是<color>.
2. 定義ViewGroup的background所需的drawbale資源:selector_viewgroup_bg.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true" android:drawable="@drawable/viewgroup_item_bg_pressed"/><item android:drawable="@drawable/viewgroup_item_bg_unpress" android:state_focused="false" android:state_pressed="false"/> </selector>3.?定義布局文件:main_activity.xml
<RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="50dp"android:clickable="true"android:background="@drawable/selector_viewgroup_bg" ><ImageViewandroid:id="@+id/img1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_centerVertical="true"android:layout_marginLeft="20dp"android:src="@drawable/more_my_collect_img" /><TextViewandroid:id="@+id/my_collect_text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_marginLeft="12dp"android:layout_toRightOf="@id/img1"android:gravity="center"android:text="我的收藏"android:textColor="#5d5d5d"android:textSize="15sp" /></RelativeLayout>
在布局文件中,我們有兩處處需要 注意:
? ·設置RelativeLayout的background屬性,指向之前定義的drawable資源selector_viewgroup_bg.xml
? ·要為RelativeLayout設置clickable 屬性:?android:clickable="true"
效果圖:
通過上述方法,即可實現(xiàn)最簡單的ViewGroup點擊效果。
源代碼下載地址(免費):http://download.csdn.net/detail/zuiwuyuan/8401989
總結(jié)
以上是生活随笔為你收集整理的Android ViewGroup点击效果(背景色)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android实现ExpandableT
- 下一篇: Android Material Des