【Android】Menu不同選單的使用介紹
【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);
}
}
On Android 2.3 and lower, the system calls
- 根據AP執行情況來調整Menu的Item
onCreateOptionsMenu()的方法只會call到一次,之後一直存在直到activity銷燬,如果我們需要在程式執行時根據不同的情況來調整menu的item話,那麼需要使用onPrepareOptionsMenu()
,這個方法傳入一個已經存在的Menu,之後我們可以進行remove,
add, disable, or enable menu items.
onPrepareOptionsMenu()
each
time the user opens the Options Menu.
On Android 3.0 and higher, you must call
【2】建立Context Menu
invalidateOptionsMenu()
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.
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.
Call
registerForContextMenu()
and
pass it theView
you want to give a context menu. When this View then receives
a long-press, it displays a context menu.Register a ListView
If your activity uses a
ListView
and
you want all list items to provide a context menu, register all items for a context menu by passing theListView
toregisterForContextMenu()
.
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
Note:Items in a context menu do not support icons or shortcut keys.
<?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 the
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 the
onOptionsItemSelected()
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:
-
Show or hide all items with
setGroupVisible()
-
Enable or disable all items with
setGroupEnabled()
-
Specify whether all items are checkable with
setGroupCheckable()
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.
<?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
android:checkable
attribute
in the<item>
element, or for an entire group with theandroid:checkableBehavior
attribute 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>
哎,今天先寫到這了,有機會下次再介紹,謝謝!
哎,今天先寫到這了,有機會下次再介紹,謝謝!
相關文章
- BootstrapBlazor實戰 Menu 導航選單使用(2)bootBlazor
- BootstrapBlazor實戰 Menu 導航選單使用(1)bootBlazor
- Tkinter (10) 選單部件 Menu
- 幽靈選單介紹;
- 介紹一個好用的 Laravel Menu 建構包Laravel
- Android NDK——初識協程(Coroutine)和libco的簡單介紹使用Android
- Android 動畫 介紹與使用Android動畫
- 修改不同IP的方法介紹!
- Android學習筆記(建立Menu,Intent的使用)Android筆記Intent
- android基礎學習-android篇day16-Menu的使用Android
- vue元件之路之menu導航選單Vue元件
- tkinter中menu選單控制元件(十二)控制元件
- Android JetPack~ LiveData (一) 介紹與使用AndroidJetpackLiveData
- Android JetPack~ ViewModel (一) 介紹與使用AndroidJetpackView
- Android執行緒池使用介紹Android執行緒
- Android入門教程 | AsyncTask 使用介紹Android
- Android 常用佈局 介紹與使用Android
- Android RxJava:基礎介紹與使用AndroidRxJava
- el-menu使用遞迴元件實現多級選單元件遞迴元件
- LSM樹的不同實現介紹
- Android最簡單的側劃選單,DrawerLayout的使用Android
- 簡單介紹nginx 變數使用Nginx變數
- 選單欄美化的好幫手Boring Old Menu Bar for MacMac
- Flownet 介紹 及光流的簡單介紹
- Boring Old Menu Bar for Mac(選單欄美化工具)Mac
- Android之Zygote介紹AndroidGo
- Relief 特徵選擇演算法簡單介紹特徵演算法
- 簡單介紹標準庫fmt的基本使用
- AOP的簡單介紹
- Webpack 的簡單介紹Web
- form表單的簡單介紹ORM
- 介紹一個 Windows 10 清理右鍵選單和新建選單的小工具 ContextMenuManagerWindowsContext
- WPF Menu控制元件 我也有個不能點菜的選單控制元件
- 簡單介紹nginx反向代理及使用Nginx
- PIR感測器選型及其使用介紹
- Windows開始選單欄軟體:DoYourData Start Menu for MacWindowsMac
- PDM選型介紹
- 簡單介紹android實現可以滑動的平滑曲線圖Android
- 簡單介紹python虛擬環境 virtualenv的使用Python