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

自定義屬性需要宣告在 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>
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>
控件專案裡若殘留用不到的 Activity、menu 等檔案,建議先清乾淨,再對專案按右鍵開啟 Properties 進行 library 設定。

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

關鍵提醒:在引用方使用這個控件時,xmlns 後面的資源命名空間一樣要填「引用方自己的專案包名」,而不是控件專案的包名,否則編譯會找不到屬性。
Android 集成支付寶與微信支付的完整流程
行動支付的普及,讓「在 App 裡收款」幾乎成了電商類應用的標配。目前最常見的組合是支付寶與微信支付兩家 SDK。本文把兩邊的接入步驟、參數注意事項與回呼處理整理成一份可
GreenDAO —— Android ORM 框架入門(一)
簡介 GreenDAO 是一個開放的 Android 程式庫,提供容易使用的 SQLite 資料庫介面,幫助開發者更高效地處理資料——將開發者從底層的資料庫操作中解放出來
Android Handler 基本用法:解決非 UI 線程更新標題的問題
從一個常見錯誤說起 許多剛開始接觸 Android 的開發者,會嘗試用 Timer 或背景線程定時更新 Activity 標題。但若在背景線程直接呼叫 setTitle,
TextSwitcher 是什麼?三步實現文本淡入淡出切換
TextSwitcher 的 Java Doc 是這樣自我介紹的: Specialized ViewSwitcher that contains only childr