Android ApiDemos樣本解析(123):Views->ImageButton

來源:互聯網
上載者:User

ImageButton 為ImageView的子類,因此可以顯示一個圖形,同時它具有Button的功能,能夠被按下並響應使用者點擊事件。ImageButton預設顯示和Button同樣風格。可以在Layout檔案中通過android:src 或是在代碼中使用setImageResource(int) 為Button指定映像。

ImageButton可以為ImageButton重新設定背景映像。並可以為Button的不同狀態(擷取焦點,失去焦點,按下)指定不同的映像,比如,綠色的映像作為預設顯示,按下時顯示黃色映像,擷取焦點時顯示橙色。一個簡單的方法是使用”selector” drawable 資源,比如:

[html] 
<?xml version=”1.0″ encoding=”utf-8″?> 
 <selector xmlns:android=”http://schemas.android.com/apk/res/android”> 
 <item android:state_pressed=”true” 
 android:drawable=”@drawable/button_pressed” /> <!– pressed –> 
 <item android:state_focused=”true” 
 android:drawable=”@drawable/button_focused” /> <!– focused –> 
 <item android:drawable=”@drawable/button_normal” /> <!– default –> 
 </selector> 

<?xml version=”1.0″ encoding=”utf-8″?>
 <selector xmlns:android=”http://schemas.android.com/apk/res/android”>
 <item android:state_pressed=”true”
 android:drawable=”@drawable/button_pressed” /> <!– pressed –>
 <item android:state_focused=”true”
 android:drawable=”@drawable/button_focused” /> <!– focused –>
 <item android:drawable=”@drawable/button_normal” /> <!– default –>
 </selector>將中個資源檔存放在/res/drawable 目錄下,然後通過android:src 為ImageButton 指定資源。 資源定義的順序非常重要。

本例使用Android系統內建的映像資源為三個按鈕指定映像:

[html] 
<ImageButton 
 android:layout_width=”100dip” 
 android:layout_height=”50dip” 
 android:src=”@android:drawable/sym_action_call” /> 
  
<ImageButton 
 android:layout_width=”wrap_content” 
 android:layout_height=”wrap_content” 
 android:src=”@android:drawable/sym_action_chat” /> 
  
<ImageButton 
 android:layout_width=”wrap_content” 
 android:layout_height=”wrap_content” 
 android:src=”@android:drawable/sym_action_email” /> 

<ImageButton
 android:layout_width=”100dip”
 android:layout_height=”50dip”
 android:src=”@android:drawable/sym_action_call” />
 
<ImageButton
 android:layout_width=”wrap_content”
 android:layout_height=”wrap_content”
 android:src=”@android:drawable/sym_action_chat” />
 
<ImageButton
 android:layout_width=”wrap_content”
 android:layout_height=”wrap_content”
 android:src=”@android:drawable/sym_action_email” />

 

 
作者:mapdigit
 

 


聯繫我們

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