623
外匯
API Demos 2.3 學習筆記 (8)-- Views->ImageButton
更多精彩內容,請點擊閱讀:《API Demos 2.3 學習筆記》
下麵我們以ImageButton為例,簡單介紹如何引用android係統內置圖標資源。
1、在java代碼中引用
在java代碼中,我們通過“android.R.drawable.圖標名稱”格式來引用係統圖標資源。具體參考如下:
private ImageButton mImageButton = null;
//通過findViewById獲得ImageButton
mImageButton = (ImageButton)findViewById(R.id.myImageButton01);
//引用android內置圖標,作為ImageButton的按鈕圖標
mImageButton.setImageResource(android.R.drawable.sym_action_call);
2、在xml布局文件中
在布局文件中,我們通常按照"@android:drawable/圖標名稱"格式來引用資源。
具體參考如下:
<ImageButton android: android:layout_width="100dip" android:layout_height="50dip" android:src="@android:drawable/sym_action_call"/>注:我們可以在AndroidSDK目錄下找到這些係統內置圖標資源,具體位置在對應Android版本的資源目錄下。以android2.3為例,這些圖標在android-sdk-linux_86/platforms/android-9/data/res/drawable-hdpi目錄下。
另外,除了引用android係統內置圖標之外,我們也可以引用自定義圖標。具體操作如下:
1、為了表示按鈕不同狀態(例如:被聚焦,被點擊等),我們可以為每種狀態定義一張圖片。首先,我們準備三張類似的圖片,放在drawable目錄下。
-
-



button_normal
button_pressed
button_focused
-
2、在drawable目錄下新建一個xml文件btn_star.xml,通過"selector"來定義正常狀態,聚焦狀態下以及按下狀態下分別顯示什麼圖標。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:andro>
<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>
3、以在xml布局文件中引用為例,在引用該自定義圖標時,圖標名稱為定義在drawable下的xml文件名稱(不包括xml後綴)。例如,上麵我們定義了btn_star.xml,引用時,可以這樣引用:"@android:drawable/btn_star"。
<ImageButton android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@android:drawable/btn_star"/>
下麵我們進行實例代碼解析:
res-layout-image_button_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- ImageButton,引用android係統內置圖標sym_action_call(撥打電話)作為按鈕圖標 -->
<ImageButton
android:
android:layout_width="100dip"
android:layout_height="50dip"
android:src="@android:drawable/sym_action_call" />
<!-- ImageButton,引用android係統內置圖標sym_action_chat(聊天)作為按鈕圖標 -->
<ImageButton
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/sym_action_chat" />
<!-- ImageButton,引用android係統內置圖標sym_action_email(郵件)作為按鈕圖標 -->
<ImageButton
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/sym_action_email" />
<!-- ImageButton,引用自定義圖標作為按鈕圖標 -->
<ImageButton
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/btn_star" />
</LinearLayout>
src-com.example.android.apis.view-ImageButton1.java
package com.example.android.apis.view;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;
import com.example.android.apis.R;
public class ImageButton1 extends Activity {
private ImageButton mImageButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_button_1);
//通過findViewById獲得ImageButton
mImageButton = (ImageButton)findViewById(R.id.myImageButton01);
//引用android內置圖標,作為ImageButton的按鈕圖標
mImageButton.setImageResource(android.R.drawable.sym_action_call);
}
}
預覽效果:

最後更新:2017-04-02 06:51:58