ActionBar 中導航菜單、內容提供中和下拉菜單

1、Action View

Action視圖是一個在ActionBar上做爲Action Button 的替代品。要聲明一個視圖,須要使用 actionLayoutactionViewClass 兩個屬性中的任意一個來分別定義視圖的佈局資源和佈局類。html

下面介紹如何定義 Searview 組件:java

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <item android:id="@+id/action_search"
          android:title="@string/action_search"
          android:icon="@drawable/ic_action_search"
          yourapp:showAsAction="ifRoom|collapseActionView"
          yourapp:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

    Note:showAsAction屬性中也包含 collapseActionView ,這是可選擇的值,聲明視圖被收縮成一個按鈕。
android

    若是想配置視圖(好比添加事件監聽),你能夠在 onCreateOptionsMenu() 函數中作這些事情。若是你想獲取視圖對象可使用靜態方法 MenuItemCompat.getActionView()app

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_activity_actions, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    // Configure the search info and add any event listeners
    ...
    return super.onCreateOptionsMenu(menu);
}

在 API  11 或者更高ide

獲取視圖對象使用 getActionView()函數函數

menu.findItem(R.id.action_search).getActionView()

更多的信息關於搜索部件看 Creating a Search Interface佈局

    處理可摺疊的Action視圖
ui

    爲了保護Action Bar的空間,你須要將你的視圖收縮成一個按鈕,這時候你就須要爲視圖添加一個 collapseActionView值給 showAsAction屬性,如上所述。spa

    當用戶選擇視圖時,系統會將視圖伸展開,你不須要回應這一項在OnOptionsItemSelected() 函數中。若是你想更新你的Activity基於你的可見的視圖,當視圖收縮和伸展時你能夠接受回調函數。
code

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.options, menu);
    MenuItem menuItem = menu.findItem(R.id.actionItem);
    ...

    // When using the support library, the setOnActionExpandListener() method is
    // static and accepts the MenuItem object as an argument
    MenuItemCompat.setOnActionExpandListener(menuItem, new OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            // Do something when collapsed
            return true;  // Return true to collapse action view
        }

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            // Do something when expanded
            return true;  // Return true to expand action view
        }
    });
}

2、Action Provider

        和 action view 相似,action provider 也是用一個自定義的佈局替代action button。然而並不徹底像 action view ,action provider 控制了全部的action的行爲,而且當按下後 action provider能夠彈出子菜單。

        爲了什麼action provider,須要在menu中的<item>標籤用一個ActionProvider的徹底限定類的類名提供 actionViewClass 屬性。

        你能夠建立你本身的acion provider 經過繼承ActionProvider類。因爲ActionProvider定義了本身的行爲,因此不須要再 onOptionsItemSelected()方法中添加監聽,若是須要你仍然能夠在onOptionsItemSelected()中設置監聽,以防你須要同時執行其餘的action,可是仍然要返回false,以便action provider 還能夠 接受onPerformDefaultAction()回調函數去執行它的意圖action。

    ShareActionProvider

    使用 ShareActionProvider 添加加一個「share」action。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
     
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
   
<item android:id="@+id/action_share"
         
android:title="@string/share"
         
yourapp:showAsAction="ifRoom"
         
yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider"
         
/>
    ...
</menu>

        如今 action provider能夠控制它的選項和處理它的顯示和動做,可是你必需要爲選項提供一個標題。剩下惟一要作的事情就是爲分享定義一個Intent。這樣作,你就要編輯onCreateOptionsMenu 方法調用MenuItemCompat.getActionProvider() 和當按下MenuItem 時的處理。而後使用返回的ShareActionProvider的 setShareIntent() 方法,而且傳遞給它一個有合適的附加內容的ACTION_SEND意圖。

       你要在onCreateOptionMuenu()初始化 share action 時調用setShareIntent() ,這是由於用戶的環境可能會變化,你必須隨時更新意圖,經過再一次調用setShareIntent() 能夠分享的內容會發生變化。

private ShareActionProvider mShareActionProvider;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_activity_actions, menu);

    // Set up ShareActionProvider's default share intent
    MenuItem shareItem = menu.findItem(R.id.action_share);
    mShareActionProvider = (ShareActionProvider)
            MenuItemCompat.getActionProvider(shareItem);
    mShareActionProvider.setShareIntent(getDefaultIntent());

    return super.onCreateOptionsMenu(menu);
}

/** Defines a default (dummy) share intent to initialize the action provider.
  * However, as soon as the actual content to be used in the intent
  * is known or changes, you must update the share intent by again calling
  * mShareActionProvider.setShareIntent()
  */
private Intent getDefaultIntent() {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("image/*");
    return intent;
}

        自定義 action provider

        建立一個你本身的 action provider 容許你能夠再次使用和管理動態的action item,Android 早已經提供了一種實現方式,經過繼承 ActionProvider 類和實現它的回調函數,重要的有如下兩個:

  • ActionProvider()

    這個構造器傳遞給你的應用程序上下文,你應該保存這個在一個成員變量裏,之後可使用在其餘的回調函數中。

  • onCreateActionView(MenuItem)

    在這裏定義視圖,從構造器裏使用 Context 獲取實例化 LayoutInflater  從XML資源中inflate  你的視圖佈局,而後綁定事件監聽。


3、Navigation Tabs

    一旦你肯定了fragment在佈局文件中出現的位置,下面是添加標籤的基本步驟:

  1. Implement the ActionBar.TabListener interface. This interface provides callbacks for tab events, such as when the user presses one so you can swap the tabs.

  2. For each tab you want to add, instantiate an ActionBar.Tab and set the ActionBar.TabListener by calling setTabListener(). Also set the tab's title and with setText() (and optionally, an icon with setIcon()).

  3. Then add each tab to the action bar by calling addTab().

/** Constructor used each time a new tab is created.
      * @param activity  The host Activity, used to instantiate the fragment
      * @param tag  The identifier tag for the fragment
      * @param clz  The fragment's Class, used to instantiate the fragment
      *//* The following are each of the  callbacks */// Check if the fragment is already initialized// If not, instantiate and add it to the activity// If it exists, simply attach it in order to show it// Detach the fragment, because another one is being attached// User selected the already selected tab. Usually do nothing.


// Notice that setContentView() is not used, because we use the root// android.R.id.content as the container for each fragment// setup action bar for tabs


4、Drop-down Navigation

下面是添加 drop-down Navigation的基本步驟

  1. Create a SpinnerAdapter that provides the list of selectable items for the drop-down and the layout to use when drawing each item in the list.

  2. Implement ActionBar.OnNavigationListener to define the behavior that occurs when the user selects an item from the list.

  3. During your activity's onCreate() method, enable the action bar's drop-down list by callingsetNavigationMode(NAVIGATION_MODE_LIST).

  4. Set the callback for the drop-down list with setListNavigationCallbacks(). For example:

    actionBar.setListNavigationCallbacks(mSpinnerAdapter, mNavigationCallback);

This method takes your SpinnerAdapter and ActionBar.OnNavigationListener.

相關文章
相關標籤/搜索