編輯:Android 程式設計入門
做 Android 專案時,每個頁面都要搭一個 title bar:不是拉一個 LinearLayout,就是拉一個 RelativeLayout。幾頁還好,頁数一多就無聊了——尤其當好幾頁只有標題文字不同、其餘完全一樣時,每頁都重刻一遍顯得程式碼冗餘,也白白增加版面渲染的負擔。
於是我寫了一個帶公共標題列的抽象 Activity,其他頁面只要繼承它即可。後來團隊重構專案時採用了這個做法(同事又幫著最佳化了一輪),確實省下不少程式碼。
網路上類似寫法很多,但都不太合用,所以自己動手。程式碼永遠可以再更好一點,這版之後還會繼續調整。
public abstract class BaseTitleActivty extends Activity implements OnClickListener {
/** 標題列容器 */
protected ViewGroup title_layout;
private LinearLayout mMianLayout;
/** 左側返回圖示 */
protected ImageView iv_common_back;
/** 左側返回文字 */
protected TextView back_text;
/** 左側自訂圖片 */
protected ImageView left_img;
/** 中間標題 */
protected TextView title_text;
/** 右側文字 */
protected TextView right_text;
/** 右側圖示 */
protected ImageView right_img;
protected View base_title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.base_title_activity);
initBaseTitle();
}
/**
* 關鍵在這裡:把子類傳進來的 layout 塞進標題列下方的容器,
* 而不是直接 setContentView 掉整個視窗。
*/
@Override
public void setContentView(int layoutResID) {
LayoutInflater inflater =
(LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(layoutResID, null);
contentView.setLayoutParams(new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
title_layout.addView(contentView);
}
/** 初始化入口 */
protected void initBaseTitle() {
initTitleView();
setTitle();
}
/** 抓取公共 title 的各個子 view */
protected void initTitleView() {
title_layout = (ViewGroup) findViewById(R.id.title_layout);
base_title = (View) findViewById(R.id.base_title);
iv_common_back = (ImageView) findViewById(R.id.iv_common_back);
back_text = (TextView) findViewById(R.id.tv_back);
left_img = (ImageView) findViewById(R.id.left_img);
title_text = (TextView) findViewById(R.id.title);
right_text = (TextView) findViewById(R.id.right_text);
right_img = (ImageView) findViewById(R.id.right_img);
iv_common_back.setOnClickListener(this);
back_text.setOnClickListener(this);
right_text.setOnClickListener(this);
right_img.setOnClickListener(this);
}
/** 留給子類覆寫,決定標題長什麼樣 */
protected void setTitle() { }
/** 只顯示返回圖示 */
protected void showTitleLeftContent() {
back_text.setText("");
}
/** 顯示返回圖示 + 文字 */
protected void showTitleLeftContent(String tv_back) {
back_text.setText(tv_back);
}
/** 左側換成一張自訂圖片 */
protected void showTitleLeftContent(int resId) {
iv_common_back.setVisibility(View.INVISIBLE);
back_text.setVisibility(View.INVISIBLE);
left_img.setVisibility(View.VISIBLE);
left_img.setImageDrawable(getResources().getDrawable(resId));
}
/** 設定標題文字與顏色 */
protected void showTitleText(String text, int colorId) {
if (!TextUtils.isEmpty(text)) {
title_text.setText(text);
}
if (colorId != 0) {
title_text.setTextColor(getResources().getColor(colorId));
}
}
/** 只設定標題文字 */
protected void showTitleText(String text) {
showTitleText(text, 0);
}
/** 只設定標題顏色 */
protected void showTitleText(int colorId) {
showTitleText("", colorId);
}
/** 用背景圖代替標題文字(例如 logo 頁) */
@SuppressLint("NewApi")
protected void showTitleBackground(int resId) {
title_text.setText("");
title_text.setBackground(getResources().getDrawable(resId));
}
/** 設定右側完整內容:文字、顏色、字級、圖示 */
protected void showTitleRightContent(String text, int colorId, int textSize, int resId) {
if (!TextUtils.isEmpty(text)) {
right_text.setVisibility(View.VISIBLE);
right_text.setText(text);
if (colorId != 0) {
right_text.setTextColor(getResources().getColor(colorId));
}
if (textSize != 0) {
right_text.setTextSize(textSize);
}
}
if (resId != 0) {
right_img.setVisibility(View.VISIBLE);
right_img.setImageDrawable(getResources().getDrawable(resId));
}
}
/** 右側:文字 + 顏色 + 字級 */
protected void showTitleRightContent(String text, int colorId, int textSize) {
showTitleRightContent(text, colorId, textSize, 0);
}
/** 右側:文字 + 顏色 */
protected void showTitleRightContent(String text, int colorId) {
showTitleRightContent(text, colorId, 0, 0);
}
/** 右側:只有文字 */
protected void showTitleRightContent(String text) {
showTitleRightContent(text, 0, 0, 0);
}
/** 右側:只有圖示 */
protected void showTitleRightContent(int resId) {
showTitleRightContent(null, 0, 0, resId);
}
}
子類只要繼承 BaseTitleActivty,在 setTitle() 裡呼叫對應方法即可:
public class OrderActivity extends BaseTitleActivty {
@Override
protected void setTitle() {
showTitleLeftContent("返回");
showTitleText("訂單詳情");
showTitleRightContent("編輯");
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_common_back:
case R.id.tv_back:
finish();
break;
case R.id.right_text:
// 處理右側按鈕
break;
default:
break;
}
}
}
setContentView 是整個方案的關鍵:子類照平常習慣寫版面,基類負責把標題列「加」在上面,而不是被替換掉。setTitle() 開成 protected 空方法,讓子類自行決定要不要客製,沒需求的頁面什麼都不寫也能跑。onClick 統一在基類掛監聽,子類只需在 onClick 裡依 id 分流,避免每頁重複掛監聽器。還可以更好的地方:目前 showTitleLeftContent(int resId) 用 setVisibility 硬切,狀態多了容易亂;比較乾淨的做法是改用 FrameLayout 疊放,或乾脆拆成「返回區」自訂 Control。另外 getResources().getColor() 在新版 SDK 已建議改用 ContextCompat.getColor()。若專案已導入 DataBinding 或 Jetpack Compose,這套繼承式封裝也可以整套換掉。
Android 橫豎屏+Fragment碎片完整實戰教學
本文透過 Fragment 碎片實現「標題列表+內容」的分欄效果,並示範如何根據橫屏、豎屏顯示不同佈局,是學習碎片與螢幕方向處理的經典範例。 項目介紹 通過碎片的方式顯
Android 內核驅動模組開發除錯筆記
在 Android 系統中,內核驅動模組是理解系統底層運作的重要起點。透過簡單的虛擬驅動範例,可以認識 proc、devfs 與 sysfs 等檔案系統接口,以及內核模組
Android 讀取設備資訊與手機號碼的實作教學
從開發者角度理解設備資訊在 Android 應用中,讀取設備資訊與手機號碼常用於設備識別、客服連繫或帳號輔助。本文以 Java 開發角度說明,重點是 TelephonyM
Android TableLayout 表格版面配置解析
TableLayout 基本概念TableLayout 適合用來建立表格型版面。每一列通常由 TableRow 組成,而欄位數量會依各列中的控制項數量決定,系統會以最大欄