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

Android 自訂標題欄(Title Bar)實作範例:左右文字與進度條

編輯:關於 Android 程式設計

預設的標題欄能放的元素很有限,實戰中常需要自訂:左邊放頁面名稱、右邊放操作提示或狀態文字,中間還可能掛一個進度條。以下示範如何透過 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE) 換上自己的標題版面。

關鍵順序:先申請,再設定內容

自訂標題必須在 setContentView() 之前呼叫 requestWindowFeature,順序寫錯會直接拿到 requestFeature() must be called before adding a content view 的例外。

package com.test;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Window;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Test extends Activity {
    boolean customTitleSupported;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 必須在設定 content view 之前檢查自訂標題是否被允許
        customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main);
        customTitleBar(getText(R.string.app_name).toString(), "hello world!!");
    }

    public void customTitleBar(String left, String right) {
        if (right.length() > 20) right = right.substring(0, 20);

        if (customTitleSupported) {
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);

            TextView titleTvLeft = (TextView) findViewById(R.id.titleTvLeft);
            TextView titleTvRight = (TextView) findViewById(R.id.titleTvRight);
            titleTvLeft.setText(left);
            titleTvRight.setText(right);

            ProgressBar titleProgressBar = (ProgressBar) findViewById(R.id.leadProgressBar);
            // 不需要進度條時就把它藏起來
            titleProgressBar.setVisibility(ProgressBar.GONE);
        }
    }
}

標題欄版面檔 title.xml

用 RelativeLayout 就能同時處理「靠左」與「靠右」兩塊文字,中間再放進度條。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="https://schemas.android.com/apk/res/android">

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/titleTvLeft"
        android:text="left"></TextView>

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/titleTvRight"
        android:text="right"
        android:layout_alignParentRight="true"></TextView>

    <ProgressBar android:id="@+id/leadProgressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/titleTvLeft"
        android:paddingLeft="3dip"></ProgressBar>

</RelativeLayout>

幾個實務提醒

  • requestWindowFeature 的回傳值代表「是否成功啟用這個 feature」,正式環境建議依此判斷,不要假設一定會成功。
  • 標題欄的高度由 title.xml 的根節點決定,通常會用 ?android:attr/windowTitleSize 對齊系統標準高度。
  • 若想連系統預設標題都不要,改用 requestWindowFeature(Window.FEATURE_NO_TITLE),並在版面檔裡完全自繪。較新的專案則建議直接改用 Toolbar/ActionBar,彈性更高也更容易維護。

以上就是自訂 Android 標題欄的完整流程,把版面檔抽成獨立 XML 之後,要換成任何設計稿都只需改 XML,不必動到 Activity 邏輯。

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