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

Android 進階實例:自訂義視窗標題(打造菜單導航列)

編輯:關於 Android 程式設計

本實例功能雖簡單,但透過擴展,可以在自訂標題中做出菜單導航等實用功能。要實現自訂視窗標題,需三個步驟:提供自訂標題的介面、將它套用給 Activity 視窗、把系統預設主題改為自訂主題。

Step 1~2:建立自訂標題版面

/res/layout/custom_title.xml:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/rectangle">
    <Button android:id="@+id/infoAtMeTextView"
        android:textColor="#FF0000"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@我" />
    <Button android:id="@+id/infoCommentTextView"
        android:textColor="#FF0000"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="評論" />
    <Button android:id="@+id/infoPrivateMsgTextView"
        android:textColor="#FF0000"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="私信" />
</LinearLayout>

Step 3:背景 shape 定義

/drawable/rectangle.xml,繪製一個帶漸層的長方形:

<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>

Step 4:套用自訂標題到 Activity

public class CustomTitleActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); // 指定使用自訂標題
        setContentView(R.layout.main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
    }
}

運行後會發現自訂 layout 無法填滿整個標題欄。查看原始碼可知,系統為 Activity 標題預設套用了 screen_title.xml,其中 ?android:attr/windowTitleSize(標題高度)、windowTitleBackgroundStyle(背景樣式)、windowContentOverlay(前景)等屬性會限制自訂版面。因此需透過自訂主題覆寫這些屬性。

Step 5:自訂主題樣式

/res/values/style.xml:

<resources>
    <!-- 繼承系統預設樣式 -->
    <style name="customTheme" parent="android:Theme">
        <!-- 標題前景設為透明 -->
        <item name="android:windowContentOverlay">@drawable/nocolor</item>
        <!-- 標題高度設為 44dp -->
        <item name="android:windowTitleSize">44dp</item>
        <!-- 標題背景樣式 -->
        <item name="android:windowTitleBackgroundStyle">@style/customBg</item>
    </style>
    <style name="customBg">
        <item name="android:background">@drawable/rectangle</item>
    </style>
</resources>

其中 @drawable/nocolor 定義於 strings.xml:

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

Step 6:套用主題到視窗

在 AndroidManifest.xml 中為 Activity 指定 android:theme="@style/customTheme":

<activity android:name=".CustomTitleActivity"
    android:theme="@style/customTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

完成以上步驟,自訂的標題列(含三個可點擊的導航按鈕)就能填滿並正常顯示。以此為基礎,可進一步加入圖示、選單或狀態切換,打造專屬的頂部導航列。

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