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

Android 自訂標題列實作與樣式覆寫詳解

編輯:關於 Android 程式設計

Android 預設的標題列相當樸素,想放圖示、狀態文字或自訂按鈕,就得自己動手。整個過程可以拆成四件事:寫一個標題版面、畫一個漸層背景、在 Activity 中掛上它,最後(若要求完美貼合)覆寫系統樣式。

步驟一:建立標題版面 res/layout/title.xml

<?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="match_parent"
        android:text="@string/hello_world"
        android:textColor="#FF00FF" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="add"
        android:text="添加" />

</LinearLayout>

步驟二:漸層背景 res/drawable/rectangle.xml

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

    <!-- 填充色為漸變色,只需 startColor 與 endColor -->
    <gradient
        android:angle="270"
        android:startColor="#A2E0FB"
        android:endColor="#1DC9CD" />

    <!-- 定義內間距 -->
    <padding
        android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp" />

</shape>

步驟三:主版面 res/layout/activity_main.xml

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

</RelativeLayout>

步驟四:在 Activity 中啟用自訂標題

package com.example.customertitle;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

// 自定義標題
public class MainActivity extends Activity {

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

        // 1. 宣告要使用自訂標題(必須在 setContentView 之前)
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_main);

        // 2. 把自訂標題的 XML 掛到視窗上
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
    }

    public void add(View v) {
        Toast.makeText(this, "按鈕被點擊", Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

這裡有個順序陷阱:requestWindowFeature() 一定要在 setContentView() 之前呼叫,否則會直接丟出例外。

發現問題:標題兩側還留著邊

部署之後你會看到自訂標題並沒有完全覆蓋,左右各留了一小塊空白。原因是系統的標題容器自帶樣式。查看 Android 原始碼中 platforms/android-8/data/res/layout/screen_custom_title.xml:

<!-- 系統窗口的界面文件 -->
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:fitsSystemWindows="true">

    <FrameLayout android:id="@android:id/title_container"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/windowTitleSize" >
    </FrameLayout>

    <FrameLayout android:id="@android:id/content"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:foregroundGravity="fill_horizontal|top"
        android:foreground="?android:attr/windowContentOverlay" />
</LinearLayout>

其中用到了三個屬性,定義在 data/res/values/themes.xml:

<style name="Theme">
    <item name="windowContentOverlay">@android:drawable/title_bar_shadow</item>
    <item name="windowTitleSize">25dip</item>
    <item name="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item>
</style>

而 WindowTitleBackground 又在 data/res/values/styles.xml 中:

<style name="WindowTitleBackground">
    <item name="android:background">@android:drawable/title_bar</item>
</style>

最終解法:繼承並覆寫主題樣式

<resources xmlns:android="https://schemas.android.com/apk/res/android">

    <!-- 定義一個樣式,覆蓋原有主題 -->
    <style name="myTheme" parent="android:Theme">
        <item name="android:windowContentOverlay">@drawable/color</item>
        <item name="android:windowTitleSize">50dp</item>
        <item name="android:windowTitleBackgroundStyle">@style/textViewBg</item>
    </style>

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

</resources>

再把這個主題掛到 AndroidManifest.xml 的 <application> 標籤上即可。顏色與字串資源的定義:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">CustomerTitle</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">自定義標題</string>

    <drawable name="color">#00000000</drawable>

</resources>

把 windowContentOverlay 換成全透明、放大 windowTitleSize、並將背景換成自己的漸層,標題列就能完整貼合邊界了。

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