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

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

圖 1. Honeycomb Gallery 應用中的操作欄,由左至右分別是 logo、導航選項標籤與操作項(右側那個懸浮的菜單按鈕)。
補充:若你想了解的是用來展示情境式操作項的 contextual action bar,請參考選單相關文件。設計規範則可查閱 Android Design 的 Action Bar 章節。


什麼是 Action overflow?對於沒有實體 MENU 鍵的手機,ActionBar 會在末端顯示一個折疊圖示,點擊後展開剩餘的選單項。依官方說法,overflow 圖示只會出現在沒有 MENU 硬鍵的機型上;有 MENU 鍵的手機則是按下實體鍵才彈出。事實上 Google 早已呼籲手機廠商與開發者淘汰 MENU 菜單,這也是 ActionBar 興起的背景。
① 經常使用:使用者高頻操作的功能,例如短信應用裡的「新建短信」。
② 重要:對使用者來說關鍵的操作,例如 Wi-Fi 設定中的「新建網絡」。
③ 典型:同類應用普遍都提供的動作,例如聯絡人應用裡的「新建聯絡人」。
在 XML 資源檔案中定義 menu,同樣支援普通菜單、二級普通菜單與二級可選項菜單。
<item
android:id="@+id/menu_about"
android:orderInCategory="2"
android:showAsAction="never"
android:title="關於"/>
android:showAsAction 的可選值有:never、always、ifRoom、withText、collapseActionView。逐一說明:
多個屬性可同時使用,以「|」分隔。
核心程式碼如下:
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 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>
在清單設定檔中,於 application 或 activity 節點加上 android:uiOptions="splitActionBarWhenNarrow" 即可:
android:name="com.steven.android23.tab5_fragmentactionbartwo.MainActivity" android:label="@string/app_name" android:uiOptions="splitActionBarWhenNarrow"
目的很明確:橫豎屏切換會導致頁面重新載入,在 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 的指引,也能讓使用者一眼找到真正需要的那個動作。
Android 加載網絡 GIF 完整解決方案:緩存、進度與同步一次搞定
前言 加載並顯示 GIF 是 App 中很常見的需求。像加載普通圖片一樣,一個完善的方案大致要滿足以下幾點: 1、自動將 GIF 下載到本地作為緩存,同一 url 第二次
Ubuntu 下用 ADB 連接小米 2 的完整設定步驟
在 Linux 上接 Android 裝置做除錯,會遇到兩層障礙:一是檔案系統的 MTP 掛載,二是 adb 需要知道廠商的 USB vendor ID。以下分兩部分處理
RecyclerView 拖動排序與側滑刪除實作
效果目標 RecyclerView 可以透過 ItemTouchHelper 實作長按拖動排序與左右滑動刪除。相比自行處理大量手勢,官方 API 更穩定,也更容易維護。
用 ListView 打造自定義表格(三):加入列合併能力
在前兩篇的基礎上,我把自定義表格又往前推了一步——現在它支援「列合併」了。整體架構拆成兩種模式:簡單模式與複雜模式,開發者可依場景自由挑選。 兩種模式的分野 簡單模式: