android imageButton 點擊按鈕前中後,按鈕顏色的變化

來源:互聯網
上載者:User

標籤:

我們在開發的過程中,往往為了美化介面的需要,會修改按鈕的預設面板,而因為Android中的按鈕有三種狀態—預設,被點擊,被選中。所以,如果要改變按鈕的外觀,需要對這三種情況都做出修改,也許在以往,我們最容易想到的就是,手動監聽按鈕的選中和點擊事件,然後寫代碼來替換按鈕的背景,但是在android中,我們不需要這麼麻煩,android早就替我們想好了解決方案,那就是selector資源。如果我們要實現按鈕的三種背景,只需在res/drawable目錄中建立這樣一個XML檔案:

  selector.xml

?
 56789 <selector xmlns:android="http://schemas.android.com/apk/res/android">        <item android:state_window_focused="false"        android:drawable="@drawable/t3"/>        <item android:state_focused="true"        android:drawable="@drawable/t1"/>        <item android:state_pressed="true"        android:drawable="@drawable/t2"/>        <item android:drawable="@drawable/t3"/></selector>

  

    正如上面的資源檔所描述的,我們在這裡定義了三種行為各自的圖片資源,接下來,我們只需要在相應的按鈕中,將背景資源指定為drawable/selector,就完成我們需要做的一切了~?

1 <ImageButton         android:layout_width="100px"        android:layout_height="50px"        android:src="@drawable/selector"        />

  這一切就這麼簡單,不用寫任何的邏輯代碼,android開發是不是很方便呢~,下面是:

 

預設:

 

點擊:

 

選中:

 

  

android imageButton 點擊按鈕前中後,按鈕顏色的變化

聯繫我們

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