編輯:關於 Android 程式設計
默認的標題欄往往不合設計稿要求——高度、背景色、按鈕位置全都不對。這時候有兩條路:一是改系統標題欄的樣式,二是乾脆不要系統標題欄,自己在佈局裡畫一個。本文兩種都試了,也踩了一個閃屏抖動的坑。
先準備兩張 png 素材:


首先必須換掉默認主題,否則會報 You cannot combine custom titles with other title features 之類的錯誤。修改 res/values/styles.xml:
最省事的做法是在默認的 AppTheme 下關掉 ActionBar:
<item name="android:windowActionBar">false</item>
更完整的做法是自定義主題:
<style name="test" parent="android:Theme">
<item name="android:windowTitleSize">50dp</item>
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
<style name="CustomWindowTitleBackground">
<item name="android:background">#8A2BE2</item>
</style>
推薦後者,因為可以同時控制標題欄高度與背景色。然後在 AndroidManifest.xml 中把應用的 theme 改成這個主題。
接著新建 res/layout/custom_title.xml 作為標題欄佈局:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- 用 layout_weight 做 1:3:1 的三段式佈局;
注意 layout_width 要寫 0dp,這樣才會按權重分配寬度 -->
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/ic_back" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="自定義標題" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/ic_more" />
</LinearLayout>
在 MainActivity 的 onCreate 中掛載:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.activity_main); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
運行起來發現一個問題:界面打開的瞬間先顯示的是沒有加載 custom_title.xml 的原始標題欄,然後才跳變成自定義樣式。
仔細看那三行代碼就能明白原因:第一行只是聲明要用自定義標題欄,並沒有加載佈局;第二行直接 setContentView 顯示了主界面,此時用的還是系統默認標題;第三行才把自定義佈局設上去。中間存在一幀的空窗,于是就閃了一下。
更麻煩的是,requestWindowFeature 必須在 setContentView 之前調用,順序不能隨意調整,所以靠調代碼順序解決不了。
思路換個方向:不要系統標題欄,直接把標題欄當作普通佈局寫進 activity_main.xml。
先在 res/values/styles.xml 的 test 主題下關掉標題:
<style name="test" parent="android:Theme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowTitleSize">50dp</item>
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
然後把標題欄佈局直接 include 進主佈局:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/custom_title"
android:layout_width="match_parent"
android:layout_height="50dp" />
<!-- 其餘內容 -->
</LinearLayout>
MainActivity 裡就不需要任何特殊處理了,把原來那兩行刪掉:
// 刪除這兩行 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
這樣一來,標題欄從第一幀起就在佈局裡,不會再有閃屏問題。
方案一的好處是不用自己處理標題文字、圖標的設置邏輯,系統幫你管;缺點是定製能力有限,而且容易出現上面那種視覺跳變。方案二完全可控,能任意放置按鈕、搜索框、Tab,也方便做成可複用的 include 佈局,代價是返回鍵、標題文字這些都要自己寫。
實際項目裡我更傾向方案二,尤其是在需要 Toolbar 效果的場景下。順帶一提,如果 targetSdk 較高,更現代的選擇是使用 Toolbar 或 ActionBar 組件配合 setSupportActionBar(),它本質上就是「關掉系統標題欄 + 自己在佈局裡放一個可定製的 ActionBar」,同時保留了菜單、返回導航等能力,比自己從零寫一個標題欄要省心。
完整的 styles.xml 參考:
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowActionBar">false</item>
</style>
<style name="CustomWindowTitleBackground">
<item name="android:background">#8A2BE2</item>
</style>
<style name="test" parent="android:Theme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowTitleSize">50dp</item>
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
Android 個人中心頭像上傳:拍照、裁切與 Base64 傳輸實作
在許多 App 的個人中心頁面中,頭像上傳是常見功能。本文整理 Android 實作頭像上傳的完整流程,包含拍照、相冊選圖、裁切、Base64 編碼與網路傳輸。 基本權限
Android 用 ListView + ImageView 實現列表單選效果
前陣子在專案中要實作列表的單選與多選效果。特別是單選,一開始因為工期比較緊,我直接用 ListView 搭配 RadioButton 來做,可能是當時考慮不夠周全,功能雖
用 Fragment 做選項卡:一次完整的動態切換實踐
本文把之前講過的「動態創建 Fragment」拿來實踐一次,做一個四頁新聞切換的選項卡。 項目布局 整體結構很簡單:上面一條橫向 LinearLayout 放四個 Tex
Android 事件分發機制圖文詳解
事件分發機制是 Android 知識體系裡繞不過去的一塊。理解透它,你才能快速定位各種「點擊沒反應」「滑動被父容器搶走」的問題,也才能寫出真正好用的自定義控件。它同時也是