編輯:關於 Android 程式設計
Android 內建的控件不可能涵蓋所有需求,這時候就得自己動手寫一個自訂控件。以下用一個實作案例完整走一遍流程。
先寫一份 layout 作為按鈕的內容組成,由一個圖示與一段文字並排構成:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="5dip"
android:paddingLeft="40dip"
android:paddingTop="5dip"
android:src="@drawable/right_icon" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dip"
android:text="確定"
android:textColor="#000000" />
</LinearLayout>
接著寫一個類別繼承 LinearLayout,把剛才的佈局 inflate 進來,再對外提供一些方法,讓呼叫端能在程式碼中控制這個自訂控件的內容:
public class ImageBtn extends LinearLayout {
private ImageView imageView;
private TextView textView;
public ImageBtn(Context context) {
super(context);
}
public ImageBtn(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.imagebtn, this);
imageView = (ImageView) findViewById(R.id.imageView1);
textView = (TextView) findViewById(R.id.textView1);
}
/** 設置圖片資源 */
public void setImageResource(int resId) {
imageView.setImageResource(resId);
}
/** 設置顯示的文字 */
public void setTextViewText(String text) {
textView.setText(text);
}
}
要在畫面上使用這個自訂控件,只需在 XML 裡寫上它的完整類別名稱即可:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<cn.com.karl.view.ImageBtn
android:id="@+id/btn_right"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/btn" />
<cn.com.karl.view.ImageBtn
android:id="@+id/btn_error"
android:layout_marginLeft="5dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/btn" />
</LinearLayout>
這裡用到了背景圖,定義在 drawable/btn.xml,透過 selector 依狀態切換樣式,能自然做出按下與聚焦的回饋:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/btn_normal"></item>
<item android:state_pressed="true"
android:drawable="@drawable/btn_white"></item>
<item android:state_checked="true"
android:drawable="@drawable/btn_white"></item>
<item android:state_focused="false"
android:state_pressed="false"
android:drawable="@drawable/btn_normal"></item>
</selector>

最後在 Activity 裡取得控件並設定內容,用法與一般控件幾乎沒有差別:
public class IdentifyButtonActivity extends Activity {
private ImageBtn imageBtn1;
private ImageBtn imageBtn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.identifybutton);
imageBtn1 = (ImageBtn) this.findViewById(R.id.btn_right);
imageBtn2 = (ImageBtn) this.findViewById(R.id.btn_error);
imageBtn1.setTextViewText("確定");
imageBtn2.setTextViewText("取消");
imageBtn1.setImageResource(R.drawable.right_icon);
imageBtn2.setImageResource(R.drawable.error_icon);
imageBtn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "點擊的正確按鈕", 1).show();
}
});
imageBtn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "點擊的錯誤按鈕", 1).show();
}
});
}
}
最後來看看自訂控件的實際效果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也歡迎多多支持本站。
Android 開發入門:Android Studio 下載與安裝全流程
Android Studio 是目前 Android 開發最主流的 IDE。本文以 Windows 環境為例,整理系統需求、下載、安裝與首次設定的完整流程,方便剛入門的開
ES 檔案瀏覽器的雲端功能怎麼用
ES 檔案瀏覽器是 Android 上相當常見的檔案管理器,新版本加入了網路相關功能,其中一項就是可以直接存取你的網盤,例如百度雲。以下示範怎麼把網盤接進來。 ES 檔案
Android 逆向入門:Apktool 與 Jadx 源碼剖析及反編譯報錯修復實錄
一、前言 在之前的破解實踐中,唯一離不開的工具就是 apktool。它有多強大不必多說,沒有它後面的工作根本無從談起。這個工具開源、用 Java 編寫、代碼結構相對清晰,
Android 讀取 assets 與 raw 檔案內容實作
Android 專案中有兩種常見的唯讀資源位置:assets 與 res/raw。assets 不一定要副檔名,也不會自動產生資源 ID;raw 則會在 res 目錄下建