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

Android 自訂標題列實作:從佈局到主題樣式的完整步驟

編輯:關於 Android 開發

Android 自訂標題列

新建 Activity 時,系統會預設給你一條樸素的標題列。能用,但很難做出品牌感。自訂標題列的好處在於可以自由安排背景、文字樣式,甚至塞進按鈕、搜尋框、頭像等元素,讓界面檔次立刻不同。整個過程分五步。

第一步:寫一個標題列的佈局

標題列本質上就是一個橫向的容器,想放什麼放什麼。這裡放一段文字和一顆「更多」按鈕:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#aa0000"
        android:text="這是我的自定義標題" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="更多" />
</LinearLayout>

第二步:定義主題樣式

在 res/values/styles.xml 中定義一個主題,並透過 windowTitleSize、windowTitleBackgroundStyle 這兩個屬性去改造標題列的外觀:

<resources>
    <style name="itcastTheme" parent="android:Theme">
        <item name="android:windowContentOverlay">@color/nonecolor</item>
        <item name="android:windowTitleSize">44dp</item><!-- 標題列高度 -->
        <item name="android:windowTitleBackgroundStyle">@style/itcastbg</item><!-- 標題列背景樣式 -->
    </style>

    <style name="itcastbg">
        <item name="android:background">@drawable/rectangle</item>
    </style>
</resources>

第三步:準備標題列背景

用 shape 定義一個漸變矩形,比一張切圖靈活得多:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <gradient
        android:angle="270"
        android:endColor="#1DC9CD"
        android:startColor="#A2E0FB" />

    <padding
        android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp" />
</shape>

第四步:在 Activity 中掛上自訂標題

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 這兩行的順序至關重要:必須在 setContentView 之前
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);

        Button mybutton = (Button) findViewById(R.id.button);
        mybutton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "盡請期待", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

這裡是最容易失敗的一環:requestWindowFeature 一定要在 setContentView 之前呼叫。一旦界面內容已經設定,再申請自訂標題就會無效,拋出運行時異常或干脆沒有變化。另外,requestWindowFeature 與 Theme 中的 windowNoTitle 屬性互斥,如果你的主題已經聲明了 NoTitle,就要先把它去掉。

第五步:在 manifest 中套用主題

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="cn.edu.hpu.activity_title"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">

        <activity
            android:name=".MainActivity"
            android:theme="@style/itcastTheme">   <!-- 關鍵:套用自訂主題 -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

五步走完,標題列就換成了你自己的樣子。補充一句實務建議:自訂標題列屬於比較早期的做法,在 Android 5.0 之後,更主流的方案是使用 Toolbar 或 ActionBar,既能獲得統一的 Material 樣式與系統交互(如溢出菜單、返回導航),也省去了 requestWindowFeature 這類容易出錯的細節。如果只是維護老專案,本文的做法仍然適用;如果是新專案,優先考慮 Toolbar。

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