編輯:進階開發
Action Bar 與 Dashboard 幾乎可以用在多數 Android 專案裡,作為介面設計的基礎圖案。
Dashboard 專案組已著手一個開源專案,目的是把可跨 UI 模板重用的程式碼收集整合起來,讓開發者更快讓專案上軌道。它以 Google I/O 會議用的 Android 應用為基礎,移除冗餘部分,保留精簡好用的片段以便理解。原始碼放在 Google Code 上。
目前這個專案做的工作會同時作用於 Dashboard 與 Action Bar。

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

不同方向使用不同布局的結構範例
<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>
橫向時把按鈕改為三欄兩列,並移除底部的即時資訊區,讓整個畫面更清爽:
<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>
Android應用程序架構入門:四大組件、Intent與進程生命週期
Android應用的基本骨架 Android應用程式由多個組件構成,最常見的是Activity、Service、Broadcast Receiver與Content Pr
Android 開發實作:在 PopupWindow 中呈現 ListView
前文我們已經談過如何在 Android 中打造帶圖片與勾選框的 ListView,那麼該怎麼把 ListView 放進 PopupWindow 裡呢?關於 ListVie
國內 Android 移動廣告平台現狀盤點
不久前我們詳細介紹過國內幾家移動廣告平台,對哇棒(wooboo)、架勢(casee)、有米(youmi)、微雲各自的優劣做過評估。本文把它們放在一起橫向對比,希望能給做移
解析 Android 樣式:布局是根本
如果你曾經是一名 Web 前台設計師,如果你曾有過設計功底,那麼你很幸運——一旦轉到 Android 平台,過去的知識和經驗都能很好地移植過來。我始終認為,學習是一個長期