【Android】Menu不同選單的使用介紹

iteye_20954發表於2011-12-28


【0】先看一段官方的說明:

Menus are an important part of an activity's user interface, which provide users a familiar way to perform actions. Android offers a simple framework for you to add standard menus to your application.

There are three types of application menus:

Options Menu
The primary collection of menu items for an activity, which appears when the user touches the MENU button. When your application is running on Android 3.0 or later, you can provide quick access to select menu items by placing them directly in theAction Bar, as "action items."
這個其實就像是這樣的選單:
Context Menu
A floating list of menu items that appears when the user touches and holds a view that's registered to provide a context menu.
這個的話其實可以理解成PC上點選右鍵的選單:

Submenu
A floating list of menu items that appears when the user touches a menu item that contains a nested menu.
這個Submenu就是子選單的意思,點選一個選單後進入子選單,有點像PC application's menu bar (File, Edit, View, etc.).

【1】使用XML檔案來建立OptionsMenu

  • 這是使用inflater XML的方式來建立一個Menu.顯然我們需要在res/menu/的目錄下建立類似下面的一個檔案:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/new_game" android:icon="@drawable/ic_new_game" android:title="@string/new_game" /> <item android:id="@+id/help" android:icon="@drawable/ic_help" android:title="@string/help" /> </menu>

android:id A resource ID that's unique to the item, which allows the application can recognize the item when the user selects it. android:icon A reference to a drawable to use as the item's icon. android:title A reference to a string to use as the item's title.除了上面三個屬性外,更多的我們可以參考Menu Resource

  • Inflating a Menu Resource
使用MenuInflater來建立一個Menu
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.game_menu, menu); return true; }在3.0之前會預設顯示6個items,如果超出6個,最後一個就用more來顯示,點選more會顯示其餘的items.
3.0之前onCreateOptionsMenu是在第一次點選menu的時候被呼叫到,而3.0之後是在activity被建立的時候就被呼叫了
當點選menu按鍵的時候會call 到onCreateOptionsMenu.
  • 當使用者點選其中的item的適合會call到下面的方法:
@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { case R.id.new_game: newGame(); return true; case R.id.help: showHelp(); return true; default: return super.onOptionsItemSelected(item); } }

  • 根據AP執行情況來調整Menu的Item
onCreateOptionsMenu()的方法只會call到一次,之後一直存在直到activity銷燬,如果我們需要在程式執行時根據不同的情況來調整menu的item話,那麼需要使用onPrepareOptionsMenu()這個方法傳入一個已經存在的Menu,之後我們可以進行remove, add, disable, or enable menu items.
On Android 2.3 and lower, the system callsonPrepareOptionsMenu()each time the user opens the Options Menu.
On Android 3.0 and higher, you must callinvalidateOptionsMenu()when you want to update the menu, because the menu is always open. The system will then callonPrepareOptionsMenu()so you can update the menu items.


【2】建立Context Menu

On Android, a context menu is displayed when the user performs a "long press" (press and hold) on an item.
我們需要使用長時間點選某個Item來顯示一個Context Menu.通常使用在ListView中比較多。
需要注意的是,我們使用Context Menu之前需要進行註冊的動作,如下描述:
In order for a View to provide a context menu, you must "register" the view for a context menu. CallregisterForContextMenu()and pass it theViewyou want to give a context menu. When this View then receives a long-press, it displays a context menu.

For example:
Register a ListView
If your activity uses aListViewand you want all list items to provide a context menu, register all items for a context menu by passing theListViewtoregisterForContextMenu(). For example, if you're using aListActivity, register all list items like this:registerForContextMenu(getListView());


實現建立Context Menu,我們需要用到onCreateContextMenu()andonContextItemSelected().
@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.context_menu, menu); }

@Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); switch (item.getItemId()) { case R.id.edit: editNote(info.id); return true; case R.id.delete: deleteNote(info.id); return true; default: return super.onContextItemSelected(item); } }
Note:Items in a context menu do not support icons or shortcut keys.

【3】Creating Submenus

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/file" android:icon="@drawable/file" android:title="@string/file" > <!-- "file" submenu --> <menu> <item android:id="@+id/create_new" android:title="@string/create_new" /> <item android:id="@+id/open" android:title="@string/open" /> </menu> </item> </menu>
When the user selects an item from a submenu, the parent menu's respective on-item-selected callback method receives the event. For instance, if the above menu is applied as an Options Menu, then theonOptionsItemSelected()method is called when a submenu item is selected.

【4】Notes

  • Menu groups

A menu group is a collection of menu items that share certain traits. With a group, you can:

You can create a group by nesting<item>elements inside a<group>element in your menu resource or by specifying a group ID with the theadd()method.

下面是一個MenuGroups的例子:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/item1"
          android:icon="@drawable/item1"
          android:title="@string/item1" />
    <!-- menu group -->
    <group android:id="@+id/group1">
        <item android:id="@+id/groupItem1"
              android:title="@string/groupItem1" />
        <item android:id="@+id/groupItem2"
              android:title="@string/groupItem2" />
    </group>
</menu>

  • Checkable menu items
You can define the checkable behavior for individual menu items using theandroid:checkableattribute in the<item>element, or for an entire group with theandroid:checkableBehaviorattribute in the<group>element. For example, all items in this menu group are checkable with a radio button:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single"> <item android:id="@+id/red" android:title="@string/red" /> <item android:id="@+id/blue" android:title="@string/blue" /> </group> </menu>
哎,今天先寫到這了,有機會下次再介紹,謝謝!



相關文章