欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android selector背景選擇器的使用詳解

 更新時間:2013年06月14日 16:31:07   作者:  
本篇文章是對Android中selector背景選擇器的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下

在開發(fā)應(yīng)用中,很多情況下要設(shè)計(jì)listview或button控件的背景,下面總結(jié)一下android的selector的用法:
1.在drawable中配置Android的selector。
將如下的XML文件保存成你自己命名的.xml文件(比如item_bg.xml),并將該文件放置在drawable文件中,在系統(tǒng)使用時根據(jù)ListView中的列表項(xiàng)的狀態(tài)來使用相應(yīng)的背景圖片。

復(fù)制代碼 代碼如下:

<?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>

2.使用上面的配置文件:
第一種方法是在listview配置文件中配置,代碼如下:android:listSelector="@drawable/item_bg"
第二種方法是在listview的item中添加屬性,代碼如下:android:background=“@drawable/item_bg"
第三種方法是在java代碼中設(shè)置,代碼如下:Drawable drawable =getResources().getDrawable(R.drawable.item_bg);
 ListView.setSelector(drawable);
上面的設(shè)置方法顯示效果有時候?yàn)楹?,所以需要在配置文件上加上如下代碼:android:cacheColorHint="@android:color/transparent" 使其背景是透明的。
同樣,Button也有一些背景效果,如下為屬性解釋:
android:state_selected是設(shè)置選中時的效果
android:state_focused是設(shè)置獲得焦點(diǎn)的效果
android:state_pressed是設(shè)置點(diǎn)擊的效果
android:state_enabled是設(shè)置是否響應(yīng)事件
下面是一個selector用來設(shè)置button中的文字狀態(tài),代碼如下:
復(fù)制代碼 代碼如下:

<?xmlversion="1.0" encoding="utf-8"?>
<selectorxmlns:android="http://schemas.android.com/apk/res/android">
    <itemandroid:state_selected="true" android:color="#FFF" />
    <itemandroid:state_focused="true" android:color="#FFF" />
    <itemandroid:state_pressed="true" android:color="#FFF" />
    <itemandroid:color="#000" />
</selector>

相關(guān)文章

最新評論