Android 深入解析selector

來源:互聯網
上載者:User

Android 深入解析selector
selector類同於shape都是xml檔案設定控制項的屬性,然後再進行引用。selector 基本屬性如下:

android:drawable="@drawable/pic2" />

android:drawable="@drawable/pic3" />

有以下幾種使用方法:

第一種是在listview中配置android:listSelector="@drawable/list_item_bg"

或者在listview的item中添加屬性android:background=“@drawable/list_item_bg"即可實現。

或者在java代碼中使用:

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

ListView.setSelector(drawable);同樣的效果。

但是這樣會出現列表有時候為黑的情況,需要加上:android:cacheColorHint="@android:color/transparent"使其透明。

Button的一些背景效果:

android:state_selected是選中

android:state_focused是獲得焦點

android:state_pressed是點擊

android:state_enabled是設定是否響應事件,指所有事件

還可以實現selector和shape的嵌套使用:

android:bottom="10dp" android:right="10dp" />

android:bottom="10dp" android:right="10dp" />




下面是一個:
如有問題請留言,若轉載註明出處。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.