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

Android ActionBar 動作欄完整解析

編輯:關於 Android 程式設計

一、ActionBar 是什麼

ActionBar(動作欄)是一個導航控制項,用來取代傳統螢幕頂端的標題欄。它固定顯示在螢幕上方,左側擺放應用圖示,右側則是操作選單的可用項目,功能上有點類似 Windows 桌面程式的工具列。

效果如下圖:

它把過去隱藏在選單裡的選項,直接提升為可見的活動項目 ActionItem(例如「設定」這類常用功能)。一句話總結:ActionBar 就是 3.0 之前 title bar 與 menu 的結合替代品。

圖 1. Honeycomb Gallery 應用中的操作欄,由左至右分別是 logo、導航選項標籤與操作項(右側那個懸浮的菜單按鈕)。

補充:若你想了解的是用來展示情境式操作項的 contextual action bar,請參考選單相關文件。設計規範則可查閱 Android Design 的 Action Bar 章節。

二、ActionBar 的四個區域

  • App Icon:可顯示應用的 icon,也能替換成其他圖示。當軟體不在最上層頁面時,圖示左側會出現一個向左箭頭,使用者可以藉此向上導航。
  • 視圖切換:提供 Tab 或下拉列表形式的頁面切換能力。
  • Action Buttons:放置最重要的功能按鈕,塞不下的會自動折入 Action overflow。
  • Action overflow:收納不常用的操作項目。

什麼是 Action overflow?對於沒有實體 MENU 鍵的手機,ActionBar 會在末端顯示一個折疊圖示,點擊後展開剩餘的選單項。依官方說法,overflow 圖示只會出現在沒有 MENU 硬鍵的機型上;有 MENU 鍵的手機則是按下實體鍵才彈出。事實上 Google 早已呼籲手機廠商與開發者淘汰 MENU 菜單,這也是 ActionBar 興起的背景。

ActionBar 的創建原則

① 經常使用:使用者高頻操作的功能,例如短信應用裡的「新建短信」。

② 重要:對使用者來說關鍵的操作,例如 Wi-Fi 設定中的「新建網絡」。

③ 典型:同類應用普遍都提供的動作,例如聯絡人應用裡的「新建聯絡人」。

三、創建 ActionBar

在 XML 資源檔案中定義 menu,同樣支援普通菜單、二級普通菜單與二級可選項菜單。

<item
    android:id="@+id/menu_about"
    android:orderInCategory="2"
    android:showAsAction="never"
    android:title="關於"/>

android:showAsAction 的可選值有:never、always、ifRoom、withText、collapseActionView。逐一說明:

  • never:不顯示在 ActionBar 上(預設值)
  • always:永遠顯示在 ActionBar 上
  • ifRoom:ActionBar 空間足夠時才顯示
  • withText:顯示在 ActionBar 上,並帶上文字標籤
  • collapseActionView:把 ActionView 折疊成普通菜單項,需 API 14 以上

多個屬性可同時使用,以「|」分隔。

四、讓 App 圖示變成可點擊的導航按鈕

核心程式碼如下:

ActionBar actionBar = this.getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent intent = new Intent(this, MainActivity.class);
            // 將 Activity 堆疊中位於 MainActivity 之上的頁面全部彈出
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            break;
        case R.id.action_search:
            break;
        default:
            return super.onOptionsItemSelected(item);
    }
    return true;
}

程式碼說明:

getActionBar() 取得 ActionBar 物件。

setDisplayHomeAsUpEnabled(true) 把 LOGO 圖示轉為可點擊按鈕,並在圖示前加上向左箭頭。

setHomeButtonEnabled(true) 把 LOGO 圖示轉為可點擊按鈕。

setDisplayShowHomeEnabled(true) 控制是否顯示 LOGO 圖示。

五、在 ActionBar 中加入 Action View

學習 ActionBar 時要掌握三件事:把選項菜單顯示成 Action Item、啟動程式 Logo 導航,以及加入 Action View。

Action View 有兩種宣告方式:在 XML 中寫 android:actionLayout="@layout/佈局名",或寫 android:actionViewClass="android.widget.SearchView"。

<item
    android:id="@+id/action_search"
    android:orderInCategory="1"
    android:showAsAction="always"
    android:title="搜索"
    android:icon="@drawable/ic_launcher"
    android:actionViewClass="android.widget.SearchView"
    android:actionLayout="@layout/search" />

<item
    android:id="@+id/action_clock"
    android:orderInCategory="2"
    android:showAsAction="always"
    android:title="時鐘"
    android:icon="@drawable/ic_launcher"
    android:actionLayout="@layout/clock" />

對應的兩個佈局檔案:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <SearchView
        android:id="@+id/search_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </SearchView>

</LinearLayout>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <AnalogClock
        android:id="@+id/analogClock_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

六、讓 ActionBar 改在螢幕底部顯示

在清單設定檔中,於 application 或 activity 節點加上 android:uiOptions="splitActionBarWhenNarrow" 即可:

android:name="com.steven.android23.tab5_fragmentactionbartwo.MainActivity"
android:label="@string/app_name"
android:uiOptions="splitActionBarWhenNarrow"

七、ActionBar 的「保護現場」

目的很明確:橫豎屏切換會導致頁面重新載入,在 tab 模式下如何記住使用者原本停在那一頁?這就需要保存與還原狀態。

@Override
protected void onSaveInstanceState(Bundle outState) {
    Log.i(TAG, "==index:" + getActionBar().getSelectedNavigationIndex());
    outState.putInt("tabindex", getActionBar().getSelectedNavigationIndex());
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    getActionBar().setSelectedNavigationItem(savedInstanceState.getInt("tabindex"));
}

八、其他常用操作

設定自訂視圖:

bar.setDisplayShowCustomEnabled(true);
bar.setDisplayHomeEnabled(false);
bar.setCustomView(R.layout.custom_view);

設定 Logo 圖示:

bar.setLogo(R.drawable.logo);

實務上,ActionBar 的空間是有限資源。與其把所有功能都塞進 Action Buttons,不如按「使用頻率 × 重要程度」排序,只保留前三到四項,其餘交給 overflow。這樣既符合 Material Design 的指引,也能讓使用者一眼找到真正需要的那個動作。

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