- A replacement for the traditional title bar at the top of the activity window. Includes:
- The application logo in the left corner.
- Provides a new interface for items in the Options Menu.
- Can provide:
- Action items
- Equal to option menu items.
- Using android:showAsAction menu item xml attribute with a value of "ifRoom"
- When there's enough room
- The menu item appears directly in the Action Bar.
- Otherwise
- The item is placed in the overflow menu, revealed by the menu icon on the right side of the Action Bar.
- By default all items from the Options Menu are placed in the overflow menu.
- The user can open by touching the menu icon on the right side of the Action Bar.
- However, you can place select menu items directly in the Action Bar as "action items," for instant access
- Action view
- Replace an action item with a widget, ex: search box.
- Using android:actionViewLayout(android:actionLayout?) attribute with a layout resource or the android:actionViewClass attribute with the class name of a widget.
- You must also declare the android:showAsAction attribute so that the item appears in the Action Bar.
- If there's not enough room in the Action Bar does not show the widget.
- Add an action to the application logo and replace it with a custom logo
- The application logo is automatically assigned the android.R.id.home ID, which the system delivers to your activity's onOptionsItemSelected() callback when touched.
- Simply respond to this ID in your callback method to perform an action such as go to your application's "home" activity.
- To replace the icon with a logo.
- Specify your application logo in the manifest file with the android:logo attribute, then call setDisplayUseLogoEnabled(true) in your activity.
- Add breadcrumbs to navigate backward through the back stack of fragments.
- Add tabs or a drop-down list to navigate through fragments.
- Customize the Action Bar with themes and backgrounds.
Summary
- action items = options menu = overflow action items (ifRoom)
- context menu: showing after long press
- Displayed when the user performs a "right-click" on a PC.
- 需覆寫 onCreateContextMenu() 及 onContextItemSelected()
- action view: widget(search box)
By the way
- Menu items could define in res/menu/filename.xml.
- Options menu
- Items are accessible in two distinct ways:
- The MENU button. (at the bottom)
- In the Action Bar (on devices running Android 3.0 or higher). (at the top)
- The primary collection of menu items for an activity.
- Cannot display a checkbox or radio button.
- Related APIs
- onCreateOptionsMenu()
- Is called only once.
- Note:
- On Android 2.3 and lower
- The system calls this to create the Options Menu when the user opens it for the first time.
- On Android 3.0 and greater
- The system creates it as soon as the activity is created, in order to populate the Action Bar.
- MenuInflater.inflate()
- onOptionsItemSelected()/android:onClick(API 11)
- onPrepareOptionsMenu()
- If you want to provide menu items that are context-sensitive to a View, use a Context Menu.
- On Android 2.3 and lower
- The system calls this each time the user opens the Options Menu.
- On Android 3.0 and higher
- You must call invalidateOptionsMenu() when you want to update the menu, because the menu is always open.
- The system will then call onPrepareOptionsMenu() so you can update the menu items.
- Context menu
- Must "register" the view for a context menu.
- Call registerForContextMenu() and pass it the View you want to give a context menu.
- Related APIs
- onCreateContextMenu()
- onContextItemSelected()
- onContextItemSelected()
- ContextMenu.ContextMenuInfo
- AdapterView.AdapterContextMenuInfo
- Includes the list ID for the selected item in the id field.
- SubMenu
- A menu that the user can open by selecting an item in another menu.
沒有留言:
張貼留言