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

Android 組合式自訂控件:以標題欄為例說明

編輯:關於 Android 程式設計

一般來說,Android 實作自訂控制項無非三種途徑:

Ⅰ、繼承現有控制項,擴充它的功能。
Ⅱ、把現有控制項組合起來,做出功能更強的複合控制項。
Ⅲ、直接重寫 View,打造一個全新的控制項。

前面已經談過用繼承的方式擴充 TextView,這次來談第二種:怎麼把現有控制項組合起來。

組合式控制項的價值在於「集合」——把幾個控制項打包成一個語意完整的單元。實作上通常是繼承一個合適的 ViewGroup,為它加上新能力,再補一批自訂屬性讓它具備擴充性。廢話不多說,就拿幾乎每個 App 都有的標題欄來當例子。

先說清楚為什麼值得抽成一個控制項:

Ⅰ、讓整個 App 的視覺風格保持一致。
Ⅱ、日後要改標題欄只需改一處,真正做到「一次編寫,到處運行」,不必逐頁修改。
Ⅲ、對外暴露接口,讓呼叫方靈活控制,功能更強。

成品長這樣:

定義自訂屬性

好的自訂控制項應該像原生控制項一樣,能讓呼叫方方便地設定屬性。做法是在 res/values 下建立 attrs.xml,宣告所需的屬性:

 <declare-styleable name="titleBar"> <attr name="title" format="string" /> <attr name="titleTextSize" format="dimension" /> <attr name="titleTextColor" format="color" /> <attr name="titleLeftText" format="string" /> <attr name="titleLeftBackground" format="color|reference" /> <attr name="titleLeftTextColor" format="color" /> <attr name="titleRightText" format="string" /> <attr name="titleRightBackground" format="color|reference" /> <attr name="titleRightTextColor" format="color" /> </declare-styleable> 

<declare-styleable> 用來標示這是一組自訂屬性,name 決定.xml 中的前綴名稱,format 則指定值的型別。各屬性對應如下:

Ⅰ、title——標題文字
Ⅱ、titleTextSize——標題文字大小
Ⅲ、titleTextColor——標題文字顏色
Ⅳ、titleLeftText——左側按鈕文字
Ⅴ、titleLeftBackground——左側按鈕背景
Ⅵ、titleLeftTextColor——左側按鈕文字顏色
Ⅶ、titleRightText——右側按鈕文字
Ⅷ、titleRightBackground——右側按鈕背景
Ⅸ、titleRightTextColor——右側按鈕文字顏色

左右按鈕的背景用 format="color|reference",用「|」分隔多種可接受型別,這樣既能填顏色值,也能填一張圖。

讀取屬性並組裝子檢視

組合式控制項通常繼承自 ViewGroup,這裡為了方便直接繼承 RelativeLayout。屬性用系統提供的 TypedArray 讀取就很省事:

 private void initAttrs(AttributeSet attrs) { TypedArray ta = this.getContext().obtainStyledAttributes(attrs, R.styleable.titleBar); if (ta != null) { title = ta.getString(R.styleable.titleBar_title); titleTextSize = ta.getDimension(R.styleable.titleBar_titleTextSize, 16); titleTextColor = ta .getColor(R.styleable.titleBar_titleTextColor, 0); titleLeftText = ta.getString(R.styleable.titleBar_titleLeftText); titleLeftBackground = ta .getDrawable(R.styleable.titleBar_titleLeftBackground); titleLeftTextColor = ta.getColor( R.styleable.titleBar_titleLeftTextColor, 0); titleRightText = ta.getString(R.styleable.titleBar_titleRightText); titleRightBackground = ta .getDrawable(R.styleable.titleBar_titleRightBackground); titleRightTextColor = ta.getColor( R.styleable.titleBar_titleRightTextColor, 0); ta.recycle(); } }

同樣地,記得呼叫 recycle() 回收資源。

屬性有了之後,就把按鈕與文字真的組起來。用 RelativeLayout 的三條規則把標題置中、左右按鈕各自靠邊:

 private void initView() { leftButton = new Button(getContext()); titleTextView = new TextView(getContext()); rightButton = new Button(getContext()); leftButton.setTextColor(titleLeftTextColor); leftButton.setBackgroundDrawable(titleLeftBackground); leftButton.setText(titleLeftText); rightButton.setTextColor(titleRightTextColor); rightButton.setBackgroundDrawable(titleRightBackground); rightButton.setText(titleRightText); titleTextView.setText(title); titleTextView.setTextSize(titleTextSize); titleTextView.setTextColor(titleTextColor); mLeftLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); mLeftLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); addView(leftButton, mLeftLayoutParams); mCenterLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); mCenterLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT); addView(titleTextView, mCenterLayoutParams); mRightLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); mRightLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); addView(rightButton, mRightLayoutParams); } 

到這一步,這個控制項已經「有血有肉」了。

對外暴露點擊接口

既然是一個共用模板,左右按鈕被點擊時該做什麼,理應由呼叫方決定,而不是寫死在控制項裡。因此定義一個接口並對外公開:

 public interface ClickListener { void Click(int tag); } private ClickListener listener; 

在模板內部為兩顆按鈕掛上監聽,再依來源分派:

 private void setListener() { leftButton.setOnClickListener(this); rightButton.setOnClickListener(this); } @Override public void onClick(View v) { if (listener != null) { if (v == leftButton) { listener.Click(LEFT_BUTTON); } else if (v == rightButton) { listener.Click(RIGHT_BUTTON); } } } 

外部只要實作這個接口,就能分辨並處理左右兩側的點擊:

 titleBar.setListener(new ClickListener() { @Override public void Click(int tag) { switch (tag) { case TitleBar.LEFT_BUTTON: Toast.makeText(MainActivity.this, "左邊按鈕被點擊了", 0).show(); break; case TitleBar.RIGHT_BUTTON: Toast.makeText(MainActivity.this, "右邊按鈕被點擊了", 0).show(); break; default: break; } } });

版面中使用

 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:custom="https://schemas.android.com/apk/res/com.example.test" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp" tools:context=".MainActivity"> <!-- <include layout="@layout/topbar" /> --> <com.example.test.TitleBar android:id="@+id/titleBar" android:layout_width="match_parent" android:layout_height="40dp" custom:titleLeftBackground="@drawable/blue_button" custom:titleLeftText="Back" custom:titleLeftTextColor="#FFFFFF" custom:titleRightBackground="@drawable/blue_button" custom:titleRightText="More" custom:titleRightTextColor="#FFFFFF" custom:title="自定義標題" custom:titleTextColor="#123412" custom:titleTextSize="10sp"/> </RelativeLayout> 

使用自訂控制項跟原生控制項有兩點不同,特別容易出錯:

Ⅰ、標籤名稱必須寫完整的類別路徑。
Ⅱ、使用自訂屬性時必須宣告自訂的命名空間:

xmlns:custom="https://schemas.android.com/apk/res/com.example.test"

命名空間後面的那串,就是你的 package 名稱,寫錯會直接編譯失敗。

運行效果:

以上就是本文的全部內容,希望對大家的學習有所幫助。

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