最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android編程簡單實(shí)現(xiàn)ImageView點(diǎn)擊時(shí)背景圖修改的方法

 更新時(shí)間:2015年12月14日 10:44:15   作者:傲慢的上校  
這篇文章主要介紹了Android編程簡單實(shí)現(xiàn)ImageView點(diǎn)擊時(shí)背景圖修改的方法,涉及Android針對背景圖相關(guān)屬性設(shè)置的操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Android編程簡單實(shí)現(xiàn)ImageView點(diǎn)擊時(shí)背景圖修改的方法。分享給大家供大家參考,具體如下:

在使用ImageView時(shí),當(dāng)被點(diǎn)擊時(shí),希望背景圖修改一下,這樣顯示被點(diǎn)擊效果明顯一些。在這里,一個(gè)很簡單的方法,最起碼是個(gè)很清晰的方法。在res/drawable文件夾下創(chuàng)建一個(gè)xml文件。比如my.xml,內(nèi)容如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
  <item android:state_pressed="true"
    android:drawable="@*android:drawable/btn_search_dialog_voice_pressed" />
  <item android:state_pressed="false"
    android:drawable="@*android:drawable/btn_search_dialog_voice_default" />
</selector>

這里面要注意一點(diǎn),就是在上面的drawable是引用的系統(tǒng)的圖片資源,如果是使用自己的圖片資源,要使用如下格式

復(fù)制代碼 代碼如下:
android:drawable="@drawable/ic_desk_point_normal"

然后,在定義imageView的xml文件里面設(shè)置:

android:src="@drawable/youPicture"
android:background="@drawable/my"

以上即可完成效果。

記得ImageView要加上加上clickable="true",不然它的selector 是不會(huì)有效果。

把下面的XML保存成.xml文件(比如list_item_bg.xml),運(yùn)行時(shí)系統(tǒng)會(huì)根據(jù)ListView中列表項(xiàng)的狀態(tài)來使用相應(yīng)的背景圖片。

drawable/list_item_bg.xml

<?xml version="1.0" encoding="utf-8" ?>   
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
 <!-- 默認(rèn)時(shí)的背景圖片 -->  
 <item android:drawable="@drawable/pic1" /> 
 <!-- 沒有焦點(diǎn)時(shí)的背景圖片 -->  
 <item android:state_window_focused="false"  
    android:drawable="@drawable/pic1" />   
 <!-- 非觸摸模式下獲得焦點(diǎn)并單擊時(shí)的背景圖片 -->  
 <item android:state_focused="true" android:state_pressed="true"   
    android:drawable= "@drawable/pic2" />  
 <!-- 觸摸模式下單擊時(shí)的背景圖片 -->  
 <item android:state_focused="false" android:state_pressed="true"   
    android:drawable="@drawable/pic3" />   
 <!--選中時(shí)的圖片背景 -->  
 <item android:state_selected="true"   
    android:drawable="@drawable/pic4" />   
 <!--獲得焦點(diǎn)時(shí)的圖片背景 -->  
 <item android:state_focused="true"   
    android:drawable="@drawable/pic5" />   
</selector>

使用方法:

第一種是在listview中配置

復(fù)制代碼 代碼如下:
android:listSelector="@drawable/list_item_bg"

第二種是在listview的item中添加屬性
復(fù)制代碼 代碼如下:
android:background="@drawable/list_item_bg"

第三種是java代碼中使用:

Drawable drawable = getResources().getDrawable(R.drawable.list_item_bg);
listview.setSelector(drawable);

注:列表有時(shí)候?yàn)楹诘那闆r,需要加上下面的代碼使其透明:

復(fù)制代碼 代碼如下:
android:cacheColorHint="@android:color/transparent"

希望本文所述對大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論

石台县| 唐河县| 应用必备| 霸州市| 临泉县| 合山市| 九龙县| 林周县| 兴仁县| 桃园县| 温泉县| 于田县| 乐山市| 安顺市| 仲巴县| 赤壁市| 五大连池市| 准格尔旗| 定州市| 龙山县| 双牌县| 永丰县| 乌拉特后旗| 锡林郭勒盟| 城步| 南川市| 库尔勒市| 瓮安县| 新乡市| 南皮县| 成武县| 阜康市| 绩溪县| 安龙县| 墨江| 新泰市| 新河县| 福建省| 富宁县| 松阳县| 勐海县|