Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 進階開發

Dashboard 與 Action Bar 在 Android 專案中的落地實作

編輯:進階開發

Action Bar 與 Dashboard 幾乎可以用在多數 Android 專案裡,作為介面設計的基礎圖案。

Dashboard 專案組已著手一個開源專案,目的是把可跨 UI 模板重用的程式碼收集整合起來,讓開發者更快讓專案上軌道。它以 Google I/O 會議用的 Android 應用為基礎,移除冗餘部分,保留精簡好用的片段以便理解。原始碼放在 Google Code 上。

目前這個專案做的工作會同時作用於 Dashboard 與 Action Bar。

實作要點:縱向與橫向要各自準備布局

讓應用同時支援直向與橫向顯示非常重要。多數布局在結構正確時可以自動適配方向,但 Dashboard 這種以固定高度區塊構成的設計做不到,因此必須撰寫兩份布局檔:一份放進 res/layout,另一份放進 res/layout-land。Android 框架會在執行時依實際方向自動挑選,不需手動判斷。

不同方向使用不同布局的結構範例

直向布局(res/layout/dashboard.xml)

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/home_root"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!-- 標題欄,即 Action Bar -->
    <LinearLayout style="@style/TitleBar">
        <ImageView style="@style/TitleBarLogo"
            android:contentDescription="@string/description_logo"
            android:src="@drawable/title_logo" />
        <View style="@style/TitleBarSpring" />
        <ImageView style="@style/TitleBarSeparator" />
        <ImageButton style="@style/TitleBarAction"
            android:id="@+id/btn_title_refresh"
            android:contentDescription="@string/description_refresh"
            android:src="@drawable/ic_title_refresh"
            android:onClick="onActionBarButtonClick" />
        <ProgressBar style="@style/TitleBarProgressIndicator"
            android:id="@+id/title_refresh_progress"
            android:visibility="gone" />
        <ImageView style="@style/TitleBarSeparator" />
        <ImageButton style="@style/TitleBarAction"
            android:contentDescription="@string/description_search"
            android:src="@drawable/ic_title_search"
            android:onClick="onActionBarButtonClick" />
    </LinearLayout>

    <!-- Dashboard 按鈕區:直向時以兩欄三列排列 -->
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="6dip">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <Button android:id="@+id/action_one_button"
                style="@style/HomeButton"
                android:onClick="onActionOneClick"
                android:text="@string/dashboard_action"
                android:drawableTop="@drawable/dashboard_button_selector" />
            <Button android:id="@+id/action_two_button"
                style="@style/HomeButton"
                android:onClick="onActionTwoClick"
                android:text="@string/dashboard_action"
                android:drawableTop="@drawable/dashboard_button_selector" />
        </LinearLayout>

        <!-- 第二列、第三列結構相同,這裡省略 -->
    </LinearLayout>

    <!-- 底部即時資訊區 -->
    <LinearLayout
        android:id="@+id/now_playing_loading"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/now_playing_height"
        android:orientation="horizontal"
        android:background="#eee"
        android:gravity="center">
        <ProgressBar
            style="?android:attr/progressBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="6dip"
            android:indeterminate="true" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/text_size_small"
            android:text="@string/now_playing_loading" />
    </LinearLayout>
</LinearLayout>

瀏覽模式(res/layout-land/dashboard.xml)

橫向時把按鈕改為三欄兩列,並移除底部的即時資訊區,讓整個畫面更清爽:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/home_root"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout style="@style/TitleBar">
        <ImageView style="@style/TitleBarLogo"
            android:src="@drawable/title_logo" />
        <View style="@style/TitleBarSpring" />
        <ImageView style="@style/TitleBarSeparator" />
        <ImageButton style="@style/TitleBarAction"
            android:id="@+id/btn_title_refresh"
            android:src="@drawable/ic_title_refresh"
            android:onClick="onActionBarButtonClick" />
        <ProgressBar style="@style/TitleBarProgressIndicator"
            android:id="@+id/title_refresh_progress"
            android:visibility="gone" />
        <ImageView style="@style/TitleBarSeparator" />
        <ImageButton style="@style/TitleBarAction"
            android:src="@drawable/ic_title_search"
            android:onClick="onActionBarButtonClick" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="6dip">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <Button android:id="@+id/action_one_button" style="@style/HomeButton"
                android:onClick="onActionOneClick" android:text="@string/dashboard_action"
                android:drawableTop="@drawable/dashboard_button_selector" />
            <Button android:id="@+id/action_two_button" style="@style/HomeButton"
                android:onClick="onActionTwoClick" android:text="@string/dashboard_action"
                android:drawableTop="@drawable/dashboard_button_selector" />
            <Button android:id="@+id/action_three_button" style="@style/HomeButton"
                android:onClick="onActionThreeClick" android:text="@string/dashboard_action"
                android:drawableTop="@drawable/dashboard_button_selector" />
        </LinearLayout>

        <!-- 第二列結構相同,這裡省略 -->
    </LinearLayout>
</LinearLayout>

其他可直接參考的專案

  • iOSched:Google I/O 官方應用,是 Dashboard 與 Action Bar 的完整實現範例,但工程龐大;若只需要這兩個元件,建議直接用 android-ui-patterns。
  • GreenDroid library:把常用 UI 元件封裝好的函式庫,能省下不少重複工作。

這個專案想達成的目標

  • 別再把時間浪費在同一段程式碼反覆複製上。
  • 讓 Android 上不同應用的操作習慣更一致。
  • 協助開發者打造功能完整、易用的應用。
  • 充分利用 Android 系統框架既有的能力。
  • 能寫在 XML 裡的就盡量寫在 XML 裡。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved