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

基于android背景選擇器selector的用法匯總

 更新時(shí)間:2013年06月14日 16:43:30   作者:  
本篇文章是對(duì)android背景選擇器selector的用法進(jìn)行了詳細(xì)的總結(jié)與分析,需要的朋友參考下
一.創(chuàng)建xml文件,位置:drawable/xxx.xml,同目錄下記得要放相關(guān)圖片
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 沒(méi)有焦點(diǎn)時(shí)的背景圖片 -->
    <item android:drawable="@drawable/handle_normal" android:state_enabled="true" android:state_window_focused="false"/>
    <!-- 觸摸模式按下時(shí)的背景圖片 -->
    <item android:drawable="@drawable/handle_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/handle_focused" android:state_enabled="true" android:state_focused="true"/>
    <item android:drawable="@drawable/handle_normal" android:state_enabled="true"/>
    <!-- 獲得焦點(diǎn)時(shí)的圖片背景 -->
    <item android:drawable="@drawable/handle_focused" android:state_focused="true"/>
    <!--
    android:state_selected是選中
 android:state_focused是獲得焦點(diǎn)
 android:state_pressed是點(diǎn)擊
 android:state_enabled是設(shè)置是否響應(yīng)事件,指所有事件
    -->
</selector>

.使用xml文件:
1.方法一:listview中配置android:listSelector="@drawable/xxx
或者在listviewitem中添加屬性android
background="@drawable/xxx"

2.方法二:Drawable drawable = getResources().getDrawable(R.drawable.xxx);  
ListView.setSelector(drawable);
但是這樣會(huì)出現(xiàn)列表有時(shí)候?yàn)楹诘那闆r,需要加上:android:cacheColorHint="@android:color/transparent"
使其透明。
相關(guān)屬性:

android:state_selected是選中
android:state_focused
是獲得焦點(diǎn)
android:state_pressed
是點(diǎn)擊
android:state_enabled
是設(shè)置是否響應(yīng)事件,指所有事件

根據(jù)這些狀態(tài)同樣可以設(shè)置buttonselector效果。也可以設(shè)置selector改變button中的文字狀態(tài)。
以下是配置button中的文字效果:
drawable/button_font.xml

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

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

Button還可以實(shí)現(xiàn)更復(fù)雜的效果,例如漸變
drawable/button_color.xml
復(fù)制代碼 代碼如下:

<SPAN style="COLOR: #330033"><?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">         /
<item android:state_pressed="true">//定義當(dāng)button 處于pressed 狀態(tài)時(shí)的形態(tài)。
                <shape>
                <gradient  android:startColor="#8600ff" />
                      <stroke   android:width="2dp" android:color="#000000" />
                       <corners android:radius="5dp" /> 
                       <padding android:left="10dp" android:top="10dp"
                                android:bottom="10dp" android:right="10dp"/> 
                 </shape>
</item>
<item android:state_focused="true">//定義當(dāng)button獲得 focus時(shí)的形態(tài)
                 <shape>
                       <gradient android:startColor="#eac100"/>
                       <stroke android:width="2dp" android:color="#333333"  color="#ffffff"/>
                       <corners android:radius="8dp" />  
                       <padding android:left="10dp" android:top="10dp"
                                android:bottom="10dp" android:right="10dp"/>                  
                </shape>
 </item>
</selector></SPAN><SPAN style="COLOR: #ff0000"> </SPAN>

最后,需要在包含 button的xml文件里添加兩項(xiàng)。例如main.xml 文件,需要在<Button />里加兩項(xiàng)android:focusable="true" android:background="@drawable/button_color"這樣就把drawale/button_color.xml文件與這個(gè)控件關(guān)聯(lián)起來(lái)了。

相關(guān)文章

最新評(píng)論

宜丰县| 大同县| 简阳市| 洮南市| 远安县| 江都市| 广东省| 尼木县| 镇安县| 屏东市| 山西省| 卓资县| 浮山县| 历史| 安徽省| 三明市| 樟树市| 西乌珠穆沁旗| 连山| 琼中| 墨玉县| 慈利县| 永城市| 延吉市| 大港区| 平乡县| 通山县| 郎溪县| 巴里| 河间市| 罗江县| 三门县| 乌兰浩特市| 丹阳市| 高要市| 通化县| 扶风县| 阆中市| 健康| 济阳县| 五家渠市|