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

Android 自定義帶按鈕的標題欄:完整實現與踩坑記錄

編輯:關於 Android 程式設計

自定義標題欄在 Android 應用裡非常常見, left 放返回鍵、中間放標題、right 放操作按鈕,幾乎是列表詳情頁的標準配置。這個功能本身不難,但涉及主題、窗口特性、布局三處配合,任何一環出錯都會報一個讓人摸不着頭腦的錯誤。以下記錄完整實現過程與踩到的坑。

一、大致流程

  1. 為指定的 Activity 設置自定義主題風格——這一步是關鍵。在 Android 4.0 以上版本中,如果主題繼續使用 Theme.Holo、Theme.Light 等,程序會一直報錯:you cannot combine custom title with other feature titles。
  2. 在 Activity 中加入三行代碼申請並設置自定義標題:
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mycustomtitle);
  1. 在 styles.xml 中定義自定義主題。實測只有使用這種寫法才不會觸發第一步的錯誤:
<resources>

    <style name="WindowTitleBackground">
        <item name="android:background">@color/blue</item>
    </style>

    <style name="MyTheme" parent="android:Theme">
        <item name="android:windowTitleSize">60dp</item>
        <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
    </style>

</resources>
  1. 用 RelativeLayout 對齊自定義標題中的各個組件。這是關鍵技巧:因為返回鍵與操作按鈕的文案長度不固定,用 LinearLayout 無法保證標題居中,而 RelativeLayout 通過 layout_toLeftOf/layout_toRightOf 讓標題自動佔據中間剩餘空間,配合 layout_centerHorizontal 即可穩定居中。

二、MainActivity 源碼

package com.gloomyfish.titledemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mycustomtitle);
    }
}

三、標題欄布局 mycustomtitle.xml

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_gravity="fill_horizontal"
    android:orientation="horizontal"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/header_left_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="5dp"
        android:layout_centerVertical="true"
        android:text="返回"
        android:textColor="#000000"/>

    <TextView
        android:id="@+id/header_text"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_toRightOf="@+id/header_left_btn"
        android:layout_toLeftOf="@+id/header_right_btn"
        android:text="My Title Bar"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="#FFFFFF"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:singleLine="true" />

    <Button
        android:id="@+id/header_right_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="5dp"
        android:layout_centerVertical="true"
        android:text="圖片"
        android:textColor="#000000"/>

</RelativeLayout>

四、別忘了 Manifest

最後在 AndroidManifest.xml 中為該 Activity 掛上自定義主題:

android:theme="@style/MyTheme"

同時要刪除 IDE 默認生成的 AppTheme 引用,否則主題衝突會導致持續報錯。

踩坑總結

回顧整個過程,三個高頻問題依次是:主題裡殘留了 Theme.Holo/Theme.Light 系列導致「不能與其他 title feature 組合」的異常;requestWindowFeature 的位置寫到了 setContentView 之後;以及 Manifest 中主題沒有真正生效。

需要說明的是,這套基於 FEATURE_CUSTOM_TITLE 的方案屬於早期寫法。在現代 Android 開發中,官方推薦直接在布局裡使用 Toolbar(或 MaterialToolbar):它本質上就是一個普通 ViewGroup,返回鍵、標題、菜單項都能通過 XML 或 menu 資源聲明,居中標題只需設置 app:titleCentered="true",配合 setSupportActionBar() 還能獲得統一的 ActionBar 能力,完全不需要折騰窗口特性與主題衝突。上述內容適合用於理解窗口裝飾機制或維護老項目。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved