Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

自訂控件實作教學:用 LinearLayout 打造可配置的圖文按鈕

編輯:關於 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 並封裝操作方法

接著寫一個類別繼承 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 中使用

最後在 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();
            }
        });
    }
}

最後來看看自訂控件的實際效果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也歡迎多多支持本站。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved