日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android ViewGroup点击效果(背景色)

發(fā)布時間:2024/1/17 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android ViewGroup点击效果(背景色) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在開發(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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。