概述
Google在開發(fā)者網(wǎng)站上關于Menu的API指南中為開發(fā)者推薦了三種基本的菜單:選項菜單(OptionsMenu)、上下文菜單(ContextMenu)和彈出菜單(PopupMenu)。下面分別給出相應的基本實現(xiàn)步驟。
選項菜單(OptionsMenu)
效果圖

實現(xiàn)步驟:
1、在res下面創(chuàng)建一個menu文件夾,并新建一個xml文件作為OptionMenu的布局文件。
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.android.peter.menudemo.MenuDemoActivity">
<item
android:id="@+id/action_1"
android:orderInCategory="100"
android:title="Option1"
app:showAsAction="never" />
<item
android:id="@+id/action_2"
android:orderInCategory="100"
android:title="Option2"
app:showAsAction="never" />
<item
android:id="@+id/action_3"
android:orderInCategory="100"
android:title="Option3"
app:showAsAction="never" />
</menu>
2、在要顯示OptionMenu的Activity里面重寫onCreateOptionsMenu和onOptionsItemSelected方法。
//創(chuàng)建菜單,加載我們之前定義的menu_main.xml布局文件
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
//當OptionsMenu被選中的時候處理具體的響應事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_1:
Toast.makeText(mContext,"Option 1",Toast.LENGTH_SHORT).show();
return true;
case R.id.action_2:
Toast.makeText(mContext,"Option 2",Toast.LENGTH_SHORT).show();
return true;
case R.id.action_3:
Toast.makeText(mContext,"Option 3",Toast.LENGTH_SHORT).show();
return true;
default:
//do nothing
}
return super.onOptionsItemSelected(item);
}
上下文菜單(ContextMenu)
效果圖

實現(xiàn)步驟
1、在res下面創(chuàng)建一個menu文件夾,并新建一個xml文件作為ContextMenu的布局文件(這里我們復用上面OptionsMenu的布局文件)。
2、在要顯示ContextMenu的Activity里面重寫onCreateContextMenu和onContextItemSelected方法。
//創(chuàng)建ContextMenu,加載我們之前定義的menu_main.xml布局文件
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.menu_main, menu);
}
//當ContextMenu被選中的時候處理具體的響應事件
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_1:
Toast.makeText(mContext,"Option 1",Toast.LENGTH_SHORT).show();
return true;
case R.id.action_2:
Toast.makeText(mContext,"Option 2",Toast.LENGTH_SHORT).show();
return true;
case R.id.action_3:
Toast.makeText(mContext,"Option 3",Toast.LENGTH_SHORT).show();
return true;
default:
//do nothing
}
return super.onContextItemSelected(item);
}
3、為控件添加長按響應屬性,并綁定這個控件。
為TextView控件添加android:longClickable屬性來響應用戶長按點擊事件。
<TextView
android:id="@+id/tv_context_menu"
android:layout_width="160dp"
android:layout_height="60dp"
android:padding="20dp"
android:text="Context Menu"
android:longClickable="true"
android:gravity="center"
/>
將View與ContextMenu綁定。
private TextView mContextMenu;
mContextMenu = findViewById(R.id.tv_context_menu);
registerForContextMenu(mContextMenu);
彈出菜單(PopupMenu)
效果圖

實現(xiàn)流程
1、在res下面創(chuàng)建一個menu文件夾,并新建一個xml文件作為PopupMenu的布局文件(這里我們復用上面OptionsMenu的布局文件)。
2、把PopupMenu相關邏輯封裝到showPopupMenu()方法中,包含PopupMenu的實例化、布局設置、顯示、添加MenuItem的點擊監(jiān)聽及響應等。
private void showPopupMenu(){
PopupMenu popupMenu = new PopupMenu(this,mPopupMenu);
popupMenu.inflate(R.menu.menu_main);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.action_1:
Toast.makeText(mContext,"Option 1",Toast.LENGTH_SHORT).show();
return true;
case R.id.action_2:
Toast.makeText(mContext,"Option 2",Toast.LENGTH_SHORT).show();
return true;
case R.id.action_3:
Toast.makeText(mContext,"Option 3",Toast.LENGTH_SHORT).show();
return true;
default:
//do nothing
}
return false;
}
});
popupMenu.show();
}
3、與View綁定,當點擊這個View的時候顯示PopupMenu。
View在xml中的布局。
<TextView
android:id="@+id/tv_popup_menu"
android:layout_width="160dp"
android:layout_height="60dp"
android:padding="20dp"
android:text="Popup Menu"
android:longClickable="true"
android:gravity="center"
android:background="@drawable/background"
/>
綁定View,點擊顯示。
private TextView mPopupMenu;
mPopupMenu = findViewById(R.id.tv_popup_menu);
mPopupMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showPopupMenu();
}
});
小結(jié)
通過比較發(fā)現(xiàn),前兩種方法實現(xiàn)起來是比較簡單的,Activity中已經(jīng)為我們提供了創(chuàng)建和響應的回調(diào)方法,我們只需要在創(chuàng)建菜單時把布局文件傳遞進去,然后在響應方法中實現(xiàn)對應項要做的事就可以了。不像第三種方法需要額外自己去創(chuàng)建實例并實現(xiàn)MenuItem的點擊監(jiān)聽。在默認顯示的位置上,OptionsMenu顯示在右上角ActionBar的位置,ContextMenu能顯示在控件視圖范圍內(nèi)任何被點擊的位置,PopupMenu與被點擊控件左下角對齊顯示。從三種菜單的實現(xiàn)效果圖可以看出,默認的菜單顯示樣式是一樣的。
菜單的實現(xiàn)方式不止這三種,更多的方法可以參看Android中的菜單實現(xiàn)匯總,開發(fā)者可以根據(jù)自己項目的需要選擇適合自己的方法實現(xiàn)菜單。