常見Android選項(xiàng)菜單樣式集合
菜單是用戶界面中最常見的元素之一,使用非常頻繁,在Android中,菜單被分為如下三種,選項(xiàng)菜單(OptionsMenu)、上下文菜單(ContextMenu)和子菜單(SubMenu),今天這講是OptionsMenu
一、概述
- public boolean onCreateOptionsMenu(Menu menu):使用此方法調(diào)用OptionsMenu 。
- public boolean onOptionsItemSelected(MenuItem item):選中菜單項(xiàng)后發(fā)生的動(dòng)作。
- public void onOptionsMenuClosed(Menu menu):菜單關(guān)閉后發(fā)生的動(dòng)作。
- public boolean onPrepareOptionsMenu(Menu menu):選項(xiàng)菜單顯示之前onPrepareOptionsMenu方法會(huì)被調(diào)用,你可以用此方法來根據(jù)打當(dāng)時(shí)的情況調(diào)整菜單。
- public boolean onMenuOpened(int featureId, Menu menu):?jiǎn)未蜷_后發(fā)生的動(dòng)作。
二、默認(rèn)樣式
默認(rèn)樣式是在屏幕底部彈出一個(gè)菜單,這個(gè)菜單我們就叫他選項(xiàng)菜單OptionsMenu,一般情況下,選項(xiàng)菜單最多顯示2排每排3個(gè)菜單項(xiàng),這些菜單項(xiàng)有文字有圖標(biāo),也被稱作Icon Menus,如果多于6項(xiàng),從第六項(xiàng)開始會(huì)被隱藏,在第六項(xiàng)會(huì)出現(xiàn)一個(gè)More里,點(diǎn)擊More才出現(xiàn)第六項(xiàng)以及以后的菜單項(xiàng),這些菜單項(xiàng)也被稱作Expanded Menus。下面介紹。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="請(qǐng)點(diǎn)擊 Menu鍵顯示選項(xiàng)菜單" android:id="@+id/TextView02" /> </LinearLayout>
2、重載onCreateOptionsMenu(Menu menu)方法
重載onCreateOptionsMenu(Menu menu)方法,并在此方法中添加菜單項(xiàng),最后返回true,如果false,菜單則不會(huì)顯示。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
/*
*
* add()方法的四個(gè)參數(shù),依次是:
*
* 1、組別,如果不分組的話就寫Menu.NONE,
*
* 2、Id,這個(gè)很重要,Android根據(jù)這個(gè)Id來確定不同的菜單
*
* 3、順序,那個(gè)菜單現(xiàn)在在前面由這個(gè)參數(shù)的大小決定
*
* 4、文本,菜單的顯示文本
*/
menu.add(Menu.NONE, Menu.FIRST + 1, 5, "刪除").setIcon(
android.R.drawable.ic_menu_delete);
// setIcon()方法為菜單設(shè)置圖標(biāo),這里使用的是系統(tǒng)自帶的圖標(biāo),同學(xué)們留意一下,以
// android.R開頭的資源是系統(tǒng)提供的,我們自己提供的資源是以R開頭的
menu.add(Menu.NONE, Menu.FIRST + 2, 2, "保存").setIcon(
android.R.drawable.ic_menu_edit);
menu.add(Menu.NONE, Menu.FIRST + 3, 6, "幫助").setIcon(
android.R.drawable.ic_menu_help);
menu.add(Menu.NONE, Menu.FIRST + 4, 1, "添加").setIcon(
android.R.drawable.ic_menu_add);
menu.add(Menu.NONE, Menu.FIRST + 5, 4, "詳細(xì)").setIcon(
android.R.drawable.ic_menu_info_details);
menu.add(Menu.NONE, Menu.FIRST + 6, 3, "發(fā)送").setIcon(
android.R.drawable.ic_menu_send);
return true;
}
3、為菜單項(xiàng)注冊(cè)事件
使用onOptionsItemSelected(MenuItem item)方法為菜單項(xiàng)注冊(cè)事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Menu.FIRST + 1:
Toast.makeText(this, "刪除菜單被點(diǎn)擊了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 2:
Toast.makeText(this, "保存菜單被點(diǎn)擊了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 3:
Toast.makeText(this, "幫助菜單被點(diǎn)擊了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 4:
Toast.makeText(this, "添加菜單被點(diǎn)擊了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 5:
Toast.makeText(this, "詳細(xì)菜單被點(diǎn)擊了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 6:
Toast.makeText(this, "發(fā)送菜單被點(diǎn)擊了", Toast.LENGTH_LONG).show();
break;
}
return false;
}
4、其他按需要重載
完整代碼
package com.wjq.menu;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class DefaultMenu extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
/*
*
* add()方法的四個(gè)參數(shù),依次是:
*
* 1、組別,如果不分組的話就寫Menu.NONE,
*
* 2、Id,這個(gè)很重要,Android根據(jù)這個(gè)Id來確定不同的菜單
*
* 3、順序,那個(gè)菜單現(xiàn)在在前面由這個(gè)參數(shù)的大小決定
*
* 4、文本,菜單的顯示文本
*/
menu.add(Menu.NONE, Menu.FIRST + 1, 5, "刪除").setIcon(
android.R.drawable.ic_menu_delete);
// setIcon()方法為菜單設(shè)置圖標(biāo),這里使用的是系統(tǒng)自帶的圖標(biāo),同學(xué)們留意一下,以
// android.R開頭的資源是系統(tǒng)提供的,我們自己提供的資源是以R開頭的
menu.add(Menu.NONE, Menu.FIRST + 2, 2, "保存").setIcon(
android.R.drawable.ic_menu_edit);
menu.add(Menu.NONE, Menu.FIRST + 3, 6, "幫助").setIcon(
android.R.drawable.ic_menu_help);
menu.add(Menu.NONE, Menu.FIRST + 4, 1, "添加").setIcon(
android.R.drawable.ic_menu_add);
menu.add(Menu.NONE, Menu.FIRST + 5, 4, "詳細(xì)").setIcon(
android.R.drawable.ic_menu_info_details);
menu.add(Menu.NONE, Menu.FIRST + 6, 3, "發(fā)送").setIcon(
android.R.drawable.ic_menu_send);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Menu.FIRST + 1:
Toast.makeText(this, "刪除菜單被點(diǎn)擊了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 2:
Toast.makeText(this, "保存菜單被點(diǎn)擊了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 3:
Toast.makeText(this, "幫助菜單被點(diǎn)擊了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 4:
Toast.makeText(this, "添加菜單被點(diǎn)擊了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 5:
Toast.makeText(this, "詳細(xì)菜單被點(diǎn)擊了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 6:
Toast.makeText(this, "發(fā)送菜單被點(diǎn)擊了", Toast.LENGTH_LONG).show();
break;
}
return false;
}
@Override
public void onOptionsMenuClosed(Menu menu) {
Toast.makeText(this, "選項(xiàng)菜單關(guān)閉了", Toast.LENGTH_LONG).show();
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
Toast.makeText(this,
"選項(xiàng)菜單顯示之前onPrepareOptionsMenu方法會(huì)被調(diào)用,你可以用此方法來根據(jù)打當(dāng)時(shí)的情況調(diào)整菜單",
Toast.LENGTH_LONG).show();
// 如果返回false,此方法就把用戶點(diǎn)擊menu的動(dòng)作給消費(fèi)了,onCreateOptionsMenu方法將不會(huì)被調(diào)用
return true;
}
}
5.效果瀏覽

三、自定義樣式
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <GridView android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:numColumns="4" android:verticalSpacing="10dip" android:horizontalSpacing="10dip" android:stretchMode="columnWidth" android:gravity="center" /> </LinearLayout>
首先自定義菜單界面,我是GridView來包含菜單項(xiàng),4列3行
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout_Item" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="5dip"> <ImageView android:id="@+id/item_image" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView> <TextView android:layout_below="@id/item_image" android:id="@+id/item_text" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="選項(xiàng)"></TextView> </RelativeLayout>
菜單項(xiàng)的現(xiàn)實(shí)樣式,一個(gè)圖標(biāo)和一個(gè)文字。
3.定義
private boolean isMore = false;// menu菜單翻頁控制
AlertDialog menuDialog;// menu菜單Dialog
GridView menuGrid;
View menuView;
private final int ITEM_SEARCH = 0;// 搜索
private final int ITEM_FILE_MANAGER = 1;// 文件管理
private final int ITEM_DOWN_MANAGER = 2;// 下載管理
private final int ITEM_FULLSCREEN = 3;// 全屏
private final int ITEM_MORE = 11;// 菜單
/** 菜單圖片 **/
int[] menu_image_array = { R.drawable.menu_search,
R.drawable.menu_filemanager, R.drawable.menu_downmanager,
R.drawable.menu_fullscreen, R.drawable.menu_inputurl,
R.drawable.menu_bookmark, R.drawable.menu_bookmark_sync_import,
R.drawable.menu_sharepage, R.drawable.menu_quit,
R.drawable.menu_nightmode, R.drawable.menu_refresh,
R.drawable.menu_more };
/** 菜單文字 **/
String[] menu_name_array = { "搜索", "文件管理", "下載管理", "全屏", "網(wǎng)址", "書簽",
"加入書簽", "分享頁面", "退出", "夜間模式", "刷新", "更多" };
/** 菜單圖片2 **/
int[] menu_image_array2 = { R.drawable.menu_auto_landscape,
R.drawable.menu_penselectmodel, R.drawable.menu_page_attr,
R.drawable.menu_novel_mode, R.drawable.menu_page_updown,
R.drawable.menu_checkupdate, R.drawable.menu_checknet,
R.drawable.menu_refreshtimer, R.drawable.menu_syssettings,
R.drawable.menu_help, R.drawable.menu_about, R.drawable.menu_return };
/** 菜單文字2 **/
String[] menu_name_array2 = { "自動(dòng)橫屏", "筆選模式", "閱讀模式", "瀏覽模式", "快捷翻頁",
"檢查更新", "檢查網(wǎng)絡(luò)", "定時(shí)刷新", "設(shè)置", "幫助", "關(guān)于", "返回" };
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
if (menuDialog == null) {
menuDialog = new AlertDialog.Builder(this).setView(menuView).show();
} else {
menuDialog.show();
}
return false;// 返回為true 則顯示系統(tǒng)menu
}
如果第一次打開則設(shè)置視圖,否則直接顯示menuDialog視圖。
private SimpleAdapter getMenuAdapter(String[] menuNameArray,
int[] imageResourceArray) {
ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < menuNameArray.length; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("itemImage", imageResourceArray[i]);
map.put("itemText", menuNameArray[i]);
data.add(map);
}
SimpleAdapter simperAdapter = new SimpleAdapter(this, data,
R.layout.item_menu, new String[] { "itemImage", "itemText" },
new int[] { R.id.item_image, R.id.item_text });
return simperAdapter;
}
為菜單添加菜單項(xiàng)。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("menu");// 必須創(chuàng)建一項(xiàng)
return super.onCreateOptionsMenu(menu);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
menuView = View.inflate(this, R.layout.gridview_menu, null);
// 創(chuàng)建AlertDialog
menuDialog = new AlertDialog.Builder(this).create();
menuDialog.setView(menuView);
menuDialog.setOnKeyListener(new OnKeyListener() {
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU)// 監(jiān)聽按鍵
dialog.dismiss();
return false;
}
});
menuGrid = (GridView) menuView.findViewById(R.id.gridview);
menuGrid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array));
/** 監(jiān)聽menu選項(xiàng) **/
menuGrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
switch (arg2) {
case ITEM_SEARCH:// 搜索
break;
case ITEM_FILE_MANAGER:// 文件管理
break;
case ITEM_DOWN_MANAGER:// 下載管理
break;
case ITEM_FULLSCREEN:// 全屏
break;
case ITEM_MORE:// 翻頁
if (isMore) {
menuGrid.setAdapter(getMenuAdapter(menu_name_array2,
menu_image_array2));
isMore = false;
} else {// 首頁
menuGrid.setAdapter(getMenuAdapter(menu_name_array,
menu_image_array));
isMore = true;
}
menuGrid.invalidate();// 更新menu
menuGrid.setSelection(ITEM_MORE);
break;
}
}
});
}
package com.wjq.menu;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
public class CustomizeMenu extends Activity {
private boolean isMore = false;// menu菜單翻頁控制
AlertDialog menuDialog;// menu菜單Dialog
GridView menuGrid;
View menuView;
private final int ITEM_SEARCH = 0;// 搜索
private final int ITEM_FILE_MANAGER = 1;// 文件管理
private final int ITEM_DOWN_MANAGER = 2;// 下載管理
private final int ITEM_FULLSCREEN = 3;// 全屏
private final int ITEM_MORE = 11;// 菜單
/** 菜單圖片 **/
int[] menu_image_array = { R.drawable.menu_search,
R.drawable.menu_filemanager, R.drawable.menu_downmanager,
R.drawable.menu_fullscreen, R.drawable.menu_inputurl,
R.drawable.menu_bookmark, R.drawable.menu_bookmark_sync_import,
R.drawable.menu_sharepage, R.drawable.menu_quit,
R.drawable.menu_nightmode, R.drawable.menu_refresh,
R.drawable.menu_more };
/** 菜單文字 **/
String[] menu_name_array = { "搜索", "文件管理", "下載管理", "全屏", "網(wǎng)址", "書簽",
"加入書簽", "分享頁面", "退出", "夜間模式", "刷新", "更多" };
/** 菜單圖片2 **/
int[] menu_image_array2 = { R.drawable.menu_auto_landscape,
R.drawable.menu_penselectmodel, R.drawable.menu_page_attr,
R.drawable.menu_novel_mode, R.drawable.menu_page_updown,
R.drawable.menu_checkupdate, R.drawable.menu_checknet,
R.drawable.menu_refreshtimer, R.drawable.menu_syssettings,
R.drawable.menu_help, R.drawable.menu_about, R.drawable.menu_return };
/** 菜單文字2 **/
String[] menu_name_array2 = { "自動(dòng)橫屏", "筆選模式", "閱讀模式", "瀏覽模式", "快捷翻頁",
"檢查更新", "檢查網(wǎng)絡(luò)", "定時(shí)刷新", "設(shè)置", "幫助", "關(guān)于", "返回" };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
menuView = View.inflate(this, R.layout.gridview_menu, null);
// 創(chuàng)建AlertDialog
menuDialog = new AlertDialog.Builder(this).create();
menuDialog.setView(menuView);
menuDialog.setOnKeyListener(new OnKeyListener() {
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU)// 監(jiān)聽按鍵
dialog.dismiss();
return false;
}
});
menuGrid = (GridView) menuView.findViewById(R.id.gridview);
menuGrid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array));
/** 監(jiān)聽menu選項(xiàng) **/
menuGrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
switch (arg2) {
case ITEM_SEARCH:// 搜索
break;
case ITEM_FILE_MANAGER:// 文件管理
break;
case ITEM_DOWN_MANAGER:// 下載管理
break;
case ITEM_FULLSCREEN:// 全屏
break;
case ITEM_MORE:// 翻頁
if (isMore) {
menuGrid.setAdapter(getMenuAdapter(menu_name_array2,
menu_image_array2));
isMore = false;
} else {// 首頁
menuGrid.setAdapter(getMenuAdapter(menu_name_array,
menu_image_array));
isMore = true;
}
menuGrid.invalidate();// 更新menu
menuGrid.setSelection(ITEM_MORE);
break;
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("menu");// 必須創(chuàng)建一項(xiàng)
return super.onCreateOptionsMenu(menu);
}
private SimpleAdapter getMenuAdapter(String[] menuNameArray,
int[] imageResourceArray) {
ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < menuNameArray.length; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("itemImage", imageResourceArray[i]);
map.put("itemText", menuNameArray[i]);
data.add(map);
}
SimpleAdapter simperAdapter = new SimpleAdapter(this, data,
R.layout.item_menu, new String[] { "itemImage", "itemText" },
new int[] { R.id.item_image, R.id.item_text });
return simperAdapter;
}
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
if (menuDialog == null) {
menuDialog = new AlertDialog.Builder(this).setView(menuView).show();
} else {
menuDialog.show();
}
return false;// 返回為true 則顯示系統(tǒng)menu
}
}
效果瀏覽:


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- Android實(shí)現(xiàn)選項(xiàng)菜單子菜單
- Android開發(fā)實(shí)現(xiàn)SubMenu選項(xiàng)菜單和子菜單示例
- Android中選項(xiàng)菜單(OptionMenu)的創(chuàng)建方法
- 基于Android實(shí)現(xiàn)點(diǎn)擊某個(gè)按鈕讓菜單選項(xiàng)從按鈕周圍指定位置彈出
- Android選項(xiàng)菜單用法實(shí)例分析
- android在連拍菜單中增加連拍張數(shù)選項(xiàng)功能實(shí)現(xiàn)代碼
- Android小程序?qū)崿F(xiàn)選項(xiàng)菜單
相關(guān)文章
Android實(shí)現(xiàn)頁面短信驗(yàn)證功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)頁面短信驗(yàn)證功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
Android 基礎(chǔ)入門教程——開發(fā)環(huán)境搭建
這篇文章主要介紹了Android 如何搭建開發(fā)環(huán)境,文中講解非常細(xì)致,幫助大家開始學(xué)習(xí)Android,想要學(xué)習(xí)Android的朋友可以了解下2020-06-06
Flutter?LinearProgressIndicator使用指南分析
這篇文章主要為大家介紹了Flutter?LinearProgressIndicator使用指南分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Android實(shí)戰(zhàn)教程第五篇之一鍵鎖屏應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Android實(shí)戰(zhàn)教程第五篇之一鍵鎖屏應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android單項(xiàng)綁定MVVM項(xiàng)目模板的方法
這篇文章主要給大家介紹了關(guān)于Android單項(xiàng)綁定MVVM項(xiàng)目模板的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Android仿QQ長(zhǎng)按彈出刪除復(fù)制框
這篇文章主要為大家詳細(xì)介紹了Android仿QQ長(zhǎng)按彈出刪除、復(fù)制、轉(zhuǎn)發(fā)框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android Java調(diào)用自己C++類庫的實(shí)例講解
今天小編就為大家分享一篇關(guān)于Android Java調(diào)用自己C++類庫的實(shí)例講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02
ScrollView與ListView合用(正確計(jì)算Listview的高度)的問題解決
最近做項(xiàng)目中用到ScrollView和ListView一起使用的問題,顯示的時(shí)候ListView不能完全正確的顯示,查了好多資料終于成功解決:2013-05-05

