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

Android 自定義標題欄的兩種做法與踩坑記錄

編輯:關於 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>
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved