概述
Google在開發(fā)者網(wǎng)站上關(guān)于Menu的API指南中為開發(fā)者推薦了三種基本的菜單:選項(xiàng)菜單(OptionsMenu)、上下文菜單(ContextMenu)和彈出菜單(PopupMenu)夫偶。下面分別給出相應(yīng)的基本實(shí)現(xiàn)步驟痹兜。
選項(xiàng)菜單(OptionsMenu)
效果圖
實(shí)現(xiàn)步驟:
1辉巡、在res下面創(chuàng)建一個(gè)menu文件夾愉耙,并新建一個(gè)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;
}
//當(dāng)OptionsMenu被選中的時(shí)候處理具體的響應(yīng)事件
@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)
效果圖
實(shí)現(xiàn)步驟
1、在res下面創(chuàng)建一個(gè)menu文件夾胶哲,并新建一個(gè)xml文件作為ContextMenu的布局文件(這里我們復(fù)用上面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);
}
//當(dāng)ContextMenu被選中的時(shí)候處理具體的響應(yīng)事件
@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把敢、為控件添加長(zhǎng)按響應(yīng)屬性,并綁定這個(gè)控件谅辣。
為TextView控件添加android:longClickable屬性來響應(yīng)用戶長(zhǎng)按點(diǎn)擊事件修赞。
<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)
效果圖
實(shí)現(xiàn)流程
1桑阶、在res下面創(chuàng)建一個(gè)menu文件夾柏副,并新建一個(gè)xml文件作為PopupMenu的布局文件(這里我們復(fù)用上面OptionsMenu的布局文件)。
2蚣录、把PopupMenu相關(guān)邏輯封裝到showPopupMenu()方法中割择,包含PopupMenu的實(shí)例化、布局設(shè)置萎河、顯示荔泳、添加MenuItem的點(diǎn)擊監(jiān)聽及響應(yīng)等。
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綁定玛歌,當(dāng)點(diǎn)擊這個(gè)View的時(shí)候顯示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,點(diǎn)擊顯示慨飘。
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)确憨,前兩種方法實(shí)現(xiàn)起來是比較簡(jiǎn)單的,Activity中已經(jīng)為我們提供了創(chuàng)建和響應(yīng)的回調(diào)方法瓤的,我們只需要在創(chuàng)建菜單時(shí)把布局文件傳遞進(jìn)去休弃,然后在響應(yīng)方法中實(shí)現(xiàn)對(duì)應(yīng)項(xiàng)要做的事就可以了。不像第三種方法需要額外自己去創(chuàng)建實(shí)例并實(shí)現(xiàn)MenuItem的點(diǎn)擊監(jiān)聽圈膏。在默認(rèn)顯示的位置上塔猾,OptionsMenu顯示在右上角ActionBar的位置,ContextMenu能顯示在控件視圖范圍內(nèi)任何被點(diǎn)擊的位置稽坤,PopupMenu與被點(diǎn)擊控件左下角對(duì)齊顯示丈甸。從三種菜單的實(shí)現(xiàn)效果圖可以看出,默認(rèn)的菜單顯示樣式是一樣的尿褪。
菜單的實(shí)現(xiàn)方式不止這三種睦擂,更多的方法可以參看Android中的菜單實(shí)現(xiàn)匯總,開發(fā)者可以根據(jù)自己項(xiàng)目的需要選擇適合自己的方法實(shí)現(xiàn)菜單杖玲。