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

Android 新手筆記(22):自定義控件的兩步走

編輯:關於 Android 程式設計

當一段佈局(比如標題欄)會在多個界面重複出現時,把它做成自定義控件是理所當然的選擇。思路很清晰:先把佈局寫好並引入,再寫一個類把事件邏輯封裝進去。

一、引入佈局文件

右鍵 res/layout 文件夾,新建 Layout Resource File 並命名為 title,內容如下:

<LinearLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="#00ff00">

    <Button
        android:id="@+id/back_button"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_gravity="center"
        android:text="返回"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/text_view"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="標題"
        android:textSize="30sp" />

    <Button
        android:id="@+id/edit_button"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_gravity="center"
        android:text="編輯"
        android:textSize="30sp" />
</LinearLayout>

順帶記住兩個容易混的屬性:android:layout_gravity 決定控件自身在父容器中的排列方式,android:gravity 決定控件內容在控件內部的排列方式。

接著在主佈局中用 include 標籤把它引入:

<LinearLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/title" />

</LinearLayout>

由於 title 自帶標題欄,引用它的 Activity 需要把系統自帶的 ActionBar 隱藏掉:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
    }
}

二、把事件邏輯封裝進來

修改 include 標籤,給它一個 id:

<LinearLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/title_bar"
        layout="@layout/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

然後新建 TitleLayout 類繼承 LinearLayout,在構造函數里完成佈局加載與按鈕事件綁定。這樣一來,外部使用者再也不需要關 internally 的控件 id:

public class TitleLayout extends LinearLayout {

    public TitleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.title, this);

        Button backButton = (Button) findViewById(R.id.back_button);
        Button editButton = (Button) findViewById(R.id.edit_button);

        backButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) getContext()).finish();
            }
        });

        editButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "你點擊了編輯按鈕", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

最後在主佈局中直接用 TitleLayout 替換原來的 include 標籤即可。這也是自定義控件最基礎的一種形式——「引入佈局 + 封裝邏輯」,後續還可以通過自定義屬性讓它變得更通用。

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