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

Android 自定義控件實戰:打造可複用的標題導航列並打包共享

編輯:關於 Android 程式設計

幾乎每個 App 都有一條頂部的標題導航列,左邊返回、中間標題、右邊操作按鈕。與其每個專案重寫一次,不如封裝成自定義 View,再打包成 library 給其他專案共用。本文示範完整的實作流程。

本文涵蓋內容

  1. 自定義屬性的定義
  2. 自定義控件的 Java 程式碼
  3. 屬性在佈局中的用法
  4. 控件專案的打包處理
  5. 其他專案的引用方式

一、定義自定義屬性

自定義屬性需要宣告在 res/values/attrs.xml 中,透過 declare-styleable 標籤把一組相關屬性綁在一起。

<resources>
    <declare-styleable name="CustomHeadView">
        <!-- 這裡依序加入 bg、text_left、text_middle、text_right、
             drawable_left、drawable_right、textSize_left 等屬性 -->
    </declare-styleable>
</resources>

二、Java 程式碼:用 TypedArray 讀取屬性

XML 上寫的屬性值,一律透過 TypedArray 取出,並在 getXxx 的第二個參數給定預設值。

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomHeadView);
id_bg = typedArray.getInt(R.styleable.CustomHeadView_bg, -1);
str_TextOfLeft = typedArray.getString(R.styleable.CustomHeadView_text_left);
str_TextOfMiddle = typedArray.getString(R.styleable.CustomHeadView_text_middle);
str_TextOfRight = typedArray.getString(R.styleable.CustomHeadView_text_right);
id_drawOfRight = typedArray.getInt(R.styleable.CustomHeadView_drawable_right, -1);
id_drawOfLeft = typedArray.getInt(R.styleable.CustomHeadView_drawable_left, -1);
f_textSizeOfLeft = typedArray.getFloat(R.styleable.CustomHeadView_textSize_left, 22);

控件的骨架如下:它繼承 RelativeLayout,在 initView 中動態 new 出三個 TextView,分別置中、靠左、靠右,並透過 LayoutParams 的 addRule 完成定位。使用完 TypedArray 後別忘了 recycle。

public class CustomHeadView extends RelativeLayout {
    private int id_bg, id_drawOfLeft, id_drawOfRight;
    private String str_TextOfLeft, str_TextOfMiddle, str_TextOfRight;
    private float f_textSizeOfLeft, f_textSizeOfMiddle, f_textSizeOfRight;
    public TextView tv_left, tv_middle, tv_right;

    public CustomHeadView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initData(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        if (id_bg != -1) setBackgroundResource(id_bg);

        tv_middle = new TextView(context);
        LayoutParams p2 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        p2.addRule(RelativeLayout.CENTER_IN_PARENT);
        tv_middle.setLayoutParams(p2);
        tv_middle.setText(str_TextOfMiddle);
        tv_middle.setTextSize(f_textSizeOfMiddle);
        tv_middle.setGravity(Gravity.CENTER);
        addView(tv_middle);

        tv_left = new TextView(context);
        LayoutParams p1 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        p1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        p1.addRule(RelativeLayout.CENTER_VERTICAL);
        tv_left.setLayoutParams(p1);
        tv_left.setText(str_TextOfLeft);
        tv_left.setTextSize(f_textSizeOfLeft);
        if (id_drawOfLeft != -1) {
            tv_left.setCompoundDrawablesWithIntrinsicBounds(
                getResources().getDrawable(id_drawOfLeft), null, null, null);
        }
        addView(tv_left);

        // tv_right 同理,靠右對齊並設定右側 drawable
        addView(tv_right);
    }

    /** 對外開放綁定點擊事件的入口 */
    public void setViewOnClickListener(View view, OnClickListener listener) {
        view.setOnClickListener(listener);
    }
}

把 tv_left、tv_right 宣告為 public,外部即可直接取用並掛上監聽器,這也是為什麼setViewOnClickListener要對外公開的原因。

三、在佈局中使用

使用前必須先在根節點宣告命名空間,而命名空間後面接的就是本專案的套件名稱(此處為 com.example.lv,請替換成你自己的 package)。

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

    <com.example.customview.CustomHeadView
        android:id="@+id/custom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        example:bg="0x7f020003"
        example:drawable_left="0x7f020001"
        example:drawable_right="0x7f020000"
        example:text_left="首頁"
        example:text_middle="主題" />
</LinearLayout>

四、打包成可共享的 library

控件專案裡若殘留用不到的 Activity、menu 等檔案,建議先清乾淨,再對專案按右鍵開啟 Properties 進行 library 設定。

五、在其他專案中引用

新專案可以透過專案參考(Project Reference)把控件專案加進來。

關鍵提醒:在引用方使用這個控件時,xmlns 後面的資源命名空間一樣要填「引用方自己的專案包名」,而不是控件專案的包名,否則編譯會找不到屬性。

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