- Xamarin Mobile Development for Android Cookbook
- Matthew Leibowitz
- 238字
- 2021-07-30 10:23:24
Adding action bar action items
The fundamental purpose of an action bar, besides navigation, is to present the user with a set of actions that can be performed.
How to do it...
By simply using the action bar, all the action items are added to the overflow:
- The XML for ActionBar items is exactly the same as the options menu:
<menu ... > <item android:id="@+id/action_refresh" android:icon="@drawable/ic_action_refresh" android:title="@string/action_refresh"/> </menu>
However, we can customize what items are displayed, and how they are displayed:
- To add action items with images to the actual
ActionBar
property, as well as more complex items, all that is needed is an attribute in the XML,showAsAction
:<menu ... xmlns:app="http://schemas.android.com/apk/res-auto"> <item ... app:showAsAction="ifRoom"/> </menu>
- If we wish to add custom views, such as a search box, to the action bar, we make use of the
actionViewClass
attribute:<menu ... xmlns:app="http://schemas.android.com/apk/res-auto"> <item ... app:actionViewClass="android.support.v7.widget.SearchView"/> </menu>
- If the view is in a layout resource file, we use the
actionLayout
attribute:<menu ... xmlns:app="http://schemas.android.com/apk/res-auto"> <item ... app:actionLayout="@layout/action_rating"/> </menu>
- Sometimes, we may wish to only display the icon initially and then, when the user taps the icon, expand the item to display the action view:
<menu ... xmlns:app="http://schemas.android.com/apk/res-auto"> <item ... app:showAsAction="ifRoom|collapseActionView"/> </menu>
How it works...
Action item buttons are just traditional options menu items but are optionally always visible on the action bar.
The underlying logic to handle item selections is the same as that for the traditional options menu. No change is required to existing code inside the OnOptionsItemSelected()
method.
The value of the showAsAction
attribute can be ifRoom
, never
, or always
. This value can optionally be combined, using a pipe, with withText
and/or collapseActionView
.
推薦閱讀
- JavaScript全程指南
- Selenium Design Patterns and Best Practices
- Python從入門到精通(精粹版)
- 羅克韋爾ControlLogix系統應用技術
- jQuery從入門到精通 (軟件開發視頻大講堂)
- 征服RIA
- Nginx Essentials
- 精通Python自然語言處理
- 用戶體驗增長:數字化·智能化·綠色化
- HTML+CSS+JavaScript網頁設計從入門到精通 (清華社"視頻大講堂"大系·網絡開發視頻大講堂)
- 零基礎學Python編程(少兒趣味版)
- 寫給大家看的Midjourney設計書
- Java高并發編程詳解:深入理解并發核心庫
- Learning Unreal Engine Game Development
- Learning Shiny