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

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

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

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

在使用ImageView時,當(dāng)被點(diǎn)擊時,希望背景圖修改一下,這樣顯示被點(diǎn)擊效果明顯一些。在這里,一個很簡單的方法,最起碼是個很清晰的方法。在res/drawable文件夾下創(chuàng)建一個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 是不會有效果。

把下面的XML保存成.xml文件(比如list_item_bg.xml),運(yùn)行時系統(tǒng)會根據(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)時的背景圖片 -->  
 <item android:drawable="@drawable/pic1" /> 
 <!-- 沒有焦點(diǎn)時的背景圖片 -->  
 <item android:state_window_focused="false"  
    android:drawable="@drawable/pic1" />   
 <!-- 非觸摸模式下獲得焦點(diǎn)并單擊時的背景圖片 -->  
 <item android:state_focused="true" android:state_pressed="true"   
    android:drawable= "@drawable/pic2" />  
 <!-- 觸摸模式下單擊時的背景圖片 -->  
 <item android:state_focused="false" android:state_pressed="true"   
    android:drawable="@drawable/pic3" />   
 <!--選中時的圖片背景 -->  
 <item android:state_selected="true"   
    android:drawable="@drawable/pic4" />   
 <!--獲得焦點(diǎn)時的圖片背景 -->  
 <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);

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

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

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

相關(guān)文章

最新評論

虹口区| 上栗县| 常宁市| 东台市| 开封市| 彭山县| 菏泽市| 鞍山市| 额济纳旗| 海丰县| 泽库县| 苍梧县| 苍溪县| 南靖县| 崇文区| 云南省| 宿松县| 焉耆| 石屏县| 郓城县| 江华| 汝南县| 平潭县| 府谷县| 常德市| 西藏| 志丹县| 利川市| 儋州市| 高台县| 泗洪县| 靖西县| 东光县| 桑植县| 聂荣县| 禄劝| 梨树县| 合川市| 格尔木市| 文水县| 芜湖市|