Android 之 selector

來源:互聯網
上載者:User

標籤:

為按鍵增加背景圖片時,需要將圖片放在 selector 供按鍵使用。
第一次擷取到 selector 的 XML 是從網上搜尋到的。一直在想,如果需要靠 Eclipse 或手動編寫 selector 時,能不能搞出來呢?
幸好,Eclipse 支援產生 selector 架構,只能說是架構,因為 selector 的內容只能手動輸入。
Eclipse 支援產生 selector 架構的步驟如下:
Eclipse Packge Explorer 中 res 上單擊右鍵 -> New -> Other... -> Android -> Android XML File:
Resource Type 選: Drawable; Root Elemant 選: seletor(檔案名稱:connect_button_press,不能大寫)
 -> 下一步 -> Finish,產生的 XML 檔案內容如下:

<?xml version="1.0" encoding="utf-8"?>  <selector xmlns:android="http://schemas.android.com/apk/res/android" >            </selector>  

按需要增加 item 項,如:

<item android:state_pressed="true" android:drawable="@drawable/net_connect" />  <item android:drawable="@drawable/net_noconnect" />  

在 Button XML 中增加:

android:background="@drawable/connect_button_press"  

在 Java 代碼中可以如下調用:

BtnNetStatus.setBackgroundResource(R.drawable.net_noconnect);  

 

Android 之 selector

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.