AndroidX 程式庫的 Toolbar 提供多種使用者與應用程式互動的方式。新增及處理動作一文說明如何定義動作,動作可以是按鈕或選單項目。本文說明如何新增兩個多功能元件:
- 動作檢視畫面是動作,可在應用程式列中提供豐富的功能。舉例來說,使用者可以在搜尋動作檢視區塊中,於應用程式列輸入搜尋文字,不必變更活動或片段。
- 動作供應器是指具有自訂版面配置的動作。動作一開始會顯示為按鈕或選單項目;使用者輕觸動作時,動作供應器會以您定義的任何方式控制動作的行為。舉例來說,動作供應器可能會在使用者輕觸時顯示選單。
AndroidX 提供多個專用的動作檢視畫面和動作供應程式小工具。舉例來說,SearchView 小工具會實作動作檢視畫面,用於輸入搜尋查詢。這個
ShareActionProvider
小工具會實作動作供應程式,以便與其他應用程式分享資訊。您也可以定義自己的動作檢視畫面和動作供應程式。
新增動作檢視畫面
如要新增動作檢視區塊,請在工具列的選單資源中建立 <item> 元素,詳情請參閱「新增及處理動作」。在 <item> 元素中新增下列其中一個屬性:
actionViewClass:實作動作的小工具類別actionLayout:版面配置資源,說明動作的元件
將 showAsAction 屬性設為 "ifRoom|collapseActionView" 或 "never|collapseActionView"。collapseActionView 旗標會指出使用者未與小工具互動時,小工具的顯示方式。如果小工具位於應用程式列,應用程式會將小工具顯示為圖示。如果小工具位於溢位選單中,應用程式會將小工具顯示為選單項目。使用者與動作檢視畫面互動時,該畫面會展開並填滿應用程式列。
舉例來說,下列程式碼會在應用程式列中新增 SearchView 小工具:
<item android:id="@+id/action_search" android:title="@string/action_search" android:icon="@drawable/ic_search" app:showAsAction="ifRoom|collapseActionView" app:actionViewClass="androidx.appcompat.widget.SearchView" />
如果使用者未與小工具互動,應用程式會將小工具顯示為 android:icon 指定的圖示。如果應用程式列沒有空間,應用程式會將動作新增至溢位選單。
使用者輕觸圖示或選單項目時,小工具會展開並填滿工具列,讓使用者與小工具互動。
如要設定動作,請在活動的 onCreateOptionsMenu() 回呼中進行。您可以呼叫 getActionView() 方法,取得動作檢視區塊的物件參照。舉例來說,以下程式碼會取得先前程式碼範例中定義的 SearchView 小工具物件參照:
Kotlin
override fun onCreateOptionsMenu(menu: Menu?): Boolean { menuInflater.inflate(R.menu.main_activity_actions, menu) val searchItem = menu?.findItem(R.id.action_search) val searchView = searchItem?.actionView as SearchView // Configure the search info and add any event listeners. return super.onCreateOptionsMenu(menu) }
Java
@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) searchItem.getActionView(); // Configure the search info and add any event listeners. return super.onCreateOptionsMenu(menu); }
回應動作檢視畫面展開事件
如果動作的 <item> 元素有 collapseActionView 標記,應用程式會將動作檢視畫面顯示為圖示,直到使用者與動作檢視畫面互動為止。使用者輕觸圖示時,onOptionsItemSelected() 的內建處理常式會展開動作檢視畫面。如果活動子類別會覆寫 onOptionsItemSelected() 方法,覆寫方法必須呼叫 super.onOptionsItemSelected(),父類別才能展開動作檢視區塊。
如要在展開或收合動作時執行某些操作,可以定義實作 MenuItem.OnActionExpandListener 的類別,並將該類別的成員傳遞至 setOnActionExpandListener()。舉例來說,您可能會想根據動作檢視畫面是展開還是收合,更新活動。下列程式碼片段說明如何定義及傳遞監聽器:
Kotlin
override fun onCreateOptionsMenu(menu: Menu?): Boolean { menuInflater.inflate(R.menu.options, menu) // Define the listener. val expandListener = object : MenuItem.OnActionExpandListener { override fun onMenuItemActionCollapse(item: MenuItem): Boolean { // Do something when the action item collapses. return true // Return true to collapse the action view. } override fun onMenuItemActionExpand(item: MenuItem): Boolean { // Do something when it expands. return true // Return true to expand the action view. } } // Get the MenuItem for the action item. val actionMenuItem = menu?.findItem(R.id.myActionItem) // Assign the listener to that action item. actionMenuItem?.setOnActionExpandListener(expandListener) // For anything else you have to do when creating the options menu, // do the following: return true }
Java
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.options, menu); // Define the listener. OnActionExpandListener expandListener = new OnActionExpandListener() { @Override public boolean onMenuItemActionCollapse(MenuItem item) { // Do something when the action item collapses. return true; // Return true to collapse action view. } @Override public boolean onMenuItemActionExpand(MenuItem item) { // Do something when it expands. return true; // Return true to expand the action view. } }; // Get the MenuItem for the action item. MenuItem actionMenuItem = menu.findItem(R.id.myActionItem); // Assign the listener to that action item. MenuItemCompat.setOnActionExpandListener(actionMenuItem, expandListener); // For anything else you have to do when creating the options menu, // do the following: return true; }
新增動作供應商
如要宣告動作供應器,請在工具列的選單資源中建立 <item> 元素,詳情請參閱「新增及處理動作」。新增 actionProviderClass 屬性,並將其設為動作提供者類別的完整類別名稱。
舉例來說,下列程式碼會宣告 ShareActionProvider,這是 AndroidX 程式庫中定義的小工具,可讓應用程式與其他應用程式共用資料:
<item android:id="@+id/action_share" android:title="@string/share" app:showAsAction="ifRoom" app:actionProviderClass="androidx.appcompat.widget.ShareActionProvider"/>
在這種情況下,由於 ShareActionProvider 會提供自己的圖形,因此不需要為小工具宣告圖示。如果您使用自訂動作,請宣告圖示。
其他資源
- 如要查看在頂端應用程式列中新增分享動作的範例,請參閱
ShareActionProvider。 - 如要進一步瞭解如何建立自訂動作供應器,請參閱
ActionProvider。