編輯:關於 Android 程式設計
Action Bar 除了放標題與操作按鈕,其實也能塞進自定義的 View。本文示範如何在 Action Bar 上加入一個自定義布局,並在其中嵌入一個 Spinner(下拉選單),點選後能觸發對應的行為。
先準備要放進 Action Bar 的布局,這裡是一個 LinearLayout,裡頭有標題文字與一個 Spinner:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/action_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#EEA9B8"
android:text="@string/action_bar_title" />
<Spinner
android:id="@+id/action_bar_spinner"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</Spinner>
</LinearLayout>
這裡直接沿用了先前抽屜導航的項目布局,讓每個選項都帶有一個圖示與一段文字:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal" >
<ImageView
android:id="@+id/drawer_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/drawer_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
布局準備好後,在 onCreate 裡把它 inflate 出來,設定好 Spinner 的 Adapter 與監聽器,最後透過 setDisplayShowCustomEnabled 與 setCustomView 讓它顯示在標題欄:
/** action_bar 添加自定義 view */
View actionbarLayout = LayoutInflater.from(this).inflate(R.layout.action_bar, null);
mActionbarSpinne = (Spinner) actionbarLayout.findViewById(R.id.action_bar_spinner);
// setAdapter 可以根據具體情況, 此處不必深究.
mActionbarSpinne.setAdapter(new SimpleAdapter(this,
getDrawerItems(true), R.layout.drawer_item, new String[] {
"drawer_img", "drawer_title" }, new int[] {
R.id.drawer_img, R.id.drawer_title }));
mActionbarSpinne.setOnItemSelectedListener(new SpinnerItemSelectedListener());
// 使自定義的普通 View 能在 title 欄顯示, actionBar.setCustomView 能起作用.
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionbarLayout);
這裡的關鍵是 setDisplayShowCustomEnabled(true) 必須先打開,setCustomView 才能生效,否則自定義的 View 不會出現在標題欄上。
接著實作 OnItemSelectedListener,在使用者選了某個項目時做出回應:
/**
* 監聽 action_bar 的 spinner item 點擊事件
*/
private class SpinnerItemSelectedListener implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> arg0, View view, int position,
long arg3) {
selectItem(view, position, MESSAGE_SPINNER_ITEM);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {}
}
selectItem 的細節如下,這裡示範當使用者選擇非第一項(position 不為 0)時,取出被點選項目的標題文字並填入某個輸入框:
if (messageId == MESSAGE_SPINNER_ITEM) {
if (position != 0) {
TextView title = (TextView) view.findViewById(R.id.drawer_title);
String cityName = title.getText().toString();
this.cityInput.setText(cityName);
}
}
透過這套做法,Action Bar 就不再只是靜態的標題列,而能搖身一變成為一個可互動的操作區,例如快速切換城市或分類,相當實用。
圖片會說話:Android 事件分發傳遞機制解析
事件衝突為什麼常發生在較複雜的 Android 版面中,常會遇到事件失效、事件被攔截或事件衝突等問題。要解決這些問題,就必須理解 Android 事件的分發與傳遞機制。事
Android 事件傳遞機制深度剖析:從源碼看事件分發與沖突處理
已經有一段時間沒寫部落格了,最近忙著趕專案,也有些迷茫不知道該寫什麼。正好最近遇到一個自訂控件的事件沖突問題,於是上網找了很多關於事件傳遞與事件沖突的解決方案,再結合 A
Android Studio 應用調試技巧(下):ADM 進階、ADB 命令與 WiFi 調試
Android Device Monitor(以下簡稱 ADM)是動態調試安卓程序的核心幫手,提供了大量實用的工具。上一章講的 log 調試,就是靠 ADM 的 logc
Android 源碼倉庫與 Repo 工具使用指南
前言 ROM Porting 這份工作瑣碎且牽涉面廣,其中「怎麼把原始碼抓下來」是入門的第一道關卡。這篇整理幾個常見名詞,以及 Repo 工具的用法。 AOSP、AOKP