編輯:關於 Android 程式設計
一個 App 裡絕大多數頁面都長得差不多:左邊返回鍵、中間標題、右邊一兩個操作入口。如果每個 Activity 都自己寫一份標題欄佈局,日後改配色、改高度、改字級就得逐頁翻。合理的做法是把標題欄抽成公共部分,各頁只專注於自己獨有的內容。

思路是把標題欄做成一個獨立佈局,透過 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE) 塞進 Window 的標題槽位,BaseActivity 攔截 setContentView(),在真正設定內容前先組好標題。
佈局 custom_common_title.xml 用水平 LinearLayout 分成五段,權重分別是 1 / 1 / 6 / 1 / 1:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/title_root_group"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@mipmap/head_bar"
android:orientation="horizontal">
<ImageView android:id="@+id/title_left_iv"
android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent" android:src="@mipmap/back" />
<TextView android:id="@+id/title_left_tv"
android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent" android:visibility="gone" />
<TextView android:id="@+id/title_name_tv"
android:layout_width="0dp" android:layout_weight="6"
android:layout_height="match_parent" />
<ImageView android:id="@+id/title_right_iv"
android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent" android:src="@mipmap/enter"
android:visibility="gone" />
<TextView android:id="@+id/title_right_tv"
android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent" />
</LinearLayout>
BaseActivity 的關鍵是覆寫三個 setContentView() 重載,讓所有設內容的入口都先經過標題處理:
public abstract class BaseActivity extends Activity {
private TextView titleLeftTV, titleNameTV, titleRightTV;
private ImageView titleLeftIV, titleRightIV;
@Override
public void setContentView(View view, ViewGroup.LayoutParams lp) {
if (hasTitle()) {
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
if (lp == null) {
super.setContentView(view);
} else {
super.setContentView(view, lp);
}
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, getCustomTitleLayoutId());
initTitle();
} else {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.setContentView(view, lp);
}
}
private void initTitle() {
titleLeftTV = (TextView) findViewById(R.id.title_left_tv);
titleLeftIV = (ImageView) findViewById(R.id.title_left_iv);
titleNameTV = (TextView) findViewById(R.id.title_name_tv);
titleRightTV = (TextView) findViewById(R.id.title_right_tv);
titleRightIV = (ImageView) findViewById(R.id.title_right_iv);
setTitleLeftView(titleLeftTV, titleLeftIV);
setTitleNameView(titleNameTV);
setTitleRightView(titleRightTV, titleRightIV);
}
// 以下四個方法留空實作,子類需要時再覆寫
public void setTitleLeftView(TextView tv, ImageView iv) { }
public void setTitleNameView(TextView tv) { }
public void setTitleRightView(TextView tv, ImageView iv) { }
protected boolean hasTitle() { return true; }
protected int getCustomTitleLayoutId() { return R.layout.custom_common_title; }
protected Drawable getTitleBg() { return null; }
}
這個設計讓子類完全不必碰標題欄:需要標題就覆寫 setTitleNameView() 設文字,不需要就讓 hasTitle() 回傳 false,想換樣式就覆寫 getCustomTitleLayoutId()。
不用 Window 的標題機制,直接在 onCreate() 裡 setContentView() 一個包含標題列的 ScrollView/LinearLayout,然後 findViewById() 抓控件。佈局 head_layout.xml:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="@mipmap/head_bar">
<ImageView android:id="@+id/title_bar_back"
android:layout_width="32dp" android:layout_height="32dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp" android:padding="5dp"
android:src="@drawable/back_select" />
<TextView android:id="@+id/base_activity_title"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="方正字體" android:textColor="#FFFFFF" android:textSize="22dp" />
<LinearLayout android:id="@+id/base_activity_right"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp" android:padding="5dp"
android:orientation="horizontal" />
</RelativeLayout>
</LinearLayout>
BaseActivity 改成用抽象方法要求子類提供必要資訊,並在 onCreate() 中統一組裝:
public abstract class NewBaseActivity extends Activity {
private TextView titleText;
private LinearLayout rightLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
initCreateView(); // 子類設定自己的佈局
initView();
initData();
initLitener();
View back = findViewById(R.id.title_bar_back);
if (back != null) { back.setOnClickListener(new MyBackListener()); }
}
protected void initView() {
rightLayout = (LinearLayout) findViewById(R.id.base_activity_right);
View rightBtn = getRightBtn();
if (rightBtn != null) { rightLayout.addView(rightBtn); }
titleText = (TextView) findViewById(R.id.base_activity_title);
if (getTopTitleText() != null) { titleText.setText(getTopTitleText()); }
}
private class MyBackListener implements View.OnClickListener {
@Override public void onClick(View v) {
mySelfDestory();
if (getLeftBtn()) {
Toast.makeText(NewBaseActivity.this, "您點擊了返回!", Toast.LENGTH_SHORT).show();
} else {
finish();
}
}
}
protected View getRightBtn() { return null; } // 子類可自訂右側按鈕
public abstract String getTopTitleText(); // 標題文字
public abstract void initCreateView(); // 設定佈局
public abstract void initData(); // 初始化資料
public abstract void initLitener(); // 設定監聽
protected boolean getLeftBtn() { return false; } // 是否攔截返回鍵
}

示範程式中,「確定」與「返回」的過場是第一種寫法,「跳轉新 BaseActivity」則走第二種。
第一種的問題很明顯:返回與確定使用的切圖在標題槽位中縮放效果很差,而且整個標題列高度被 Window 機制鎖住,無法自由調整。第二種把標題欄還原成一個普通 View,高度、內距、背景都能隨意控制,代价是每個 Activity 都要自己 include 一次標題佈局(也可以再包一層工具方法解決)。
因此實務上推薦第二種。若願意再往前一步,可以把它寫成自定義 TitleBar extends LinearLayout,內部自帶返回監聽與標題設定方法,並透過自定義屬性在 XML 中直接指定標題文字與是否顯示返回鍵——那樣連 include 都省了,才是這個方向最自然的終點。
Android 以反射與註解簡化程式碼(一)
當應用越來越複雜時,開發者常會希望減少重複程式碼,或讓部分功能可以動態載入。反射與註解是 Java 與 Android 中常見的兩種工具,前者可在執行期取得類別資訊,後者
Android 傳感器入門:分類、坐標系、核心 API 與開發避坑指南
接下來我們進入 Android 的一些進階主題:傳感器、GPS、NFC、語音與人臉識別等。本文先從傳感器開始,把分類、坐標系、框架 API 與實務注意事項一次講清楚。
如何將 Android 資料庫操作通用化:從手寫 Dao 到映射設計
Android 的 SQLite 操作若每個表都手寫 Dao,會產生大量重複程式碼。要通用化,關鍵在於讓實體類、資料表、欄位與主鍵之間能自動映射。 從 Web 端 ORM
Android 筆記(十一):深入理解 ListView 與 Adapter
一、ListView 概覽 ListView 是 Android 開發中相當常見的元件,它以列表形式呈現資料。一般來說,一個 ListView 由三個部分組成: 1. V