搜尋此網誌

2014年11月25日 星期二

【Android】動態Menu - onPrepareOptionsMenu

ref:
[Android]Android動態menu選單之實作演練(onPrepareOptionsMenu)
Change options menu during runtime - invalidateOptionsMenu()
Android Developer- Menus


在運行時改變menu item的狀態 
onCreateOptionsMenu()此方法是控制進入該頁面之後首次點擊Menu時會出現的選單項目,此方法只會被呼叫一次。而onPrepareOptionsMenu()此方法是每次點擊Menu時都會被呼叫一次,所以在此方法中,判斷事件的發生後將需要變化的選單加入便可以達到動態menu選單的效果。

Where the items in your options menu appear on the screen depends on the version for which you've developed your application:
  • If you've developed your application for Android 2.3.x (API level 10) or lower, the contents of your options menu appear at the bottom of the screen when the user presses the Menu button, as shown in figure 1. When opened, the first visible portion is the icon menu, which holds up to six menu items. If your menu includes more than six items, Android places the sixth item and the rest into the overflow menu, which the user can open by selecting More.
  • If you've developed your application for Android 3.0 (API level 11) and higher, items from the options menu are available in the action bar. By default, the system places all items in the action overflow, which the user can reveal with the action overflow icon on the right side of the action bar (or by pressing the device Menu button, if available). To enable quick access to important actions, you can promote a few items to appear in the action bar by adding android:showAsAction="ifRoom" to the corresponding <item> elements (see figure 2).

沒有留言: