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

Android Action Bar 自定義 View:把 Spinner 放進標題欄

編輯:關於 Android 程式設計

Action Bar 除了放標題與操作按鈕,其實也能塞進自定義的 View。本文示範如何在 Action Bar 上加入一個自定義布局,並在其中嵌入一個 Spinner(下拉選單),點選後能觸發對應的行為。

一、自定義布局 action_bar.xml

先準備要放進 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>

二、Spinner 項目布局 drawer_item.xml

這裡直接沿用了先前抽屜導航的項目布局,讓每個選項都帶有一個圖示與一段文字:

<?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>

三、在 Activity 中掛載自定義 View

布局準備好後,在 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 不會出現在標題欄上。

四、監聽 Spinner 的選擇事件

接著實作 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 就不再只是靜態的標題列,而能搖身一變成為一個可互動的操作區,例如快速切換城市或分類,相當實用。

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