欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

常見Android選項(xiàng)菜單樣式集合

 更新時(shí)間:2020年05月22日 11:25:33   作者:ForrestWoo  
這篇文章主要為大家分享了一份屬于你自己的常見Android菜單樣式集合,方便大家開發(fā)使用Android菜單,對(duì)OptionMenu感興趣的小伙伴們可以參考一下

菜單是用戶界面中最常見的元素之一,使用非常頻繁,在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)用,你可以用此方法來(lái)根據(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來(lái)確定不同的菜單
 * 
 * 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來(lái)確定不同的菜單
 * 
 * 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)用,你可以用此方法來(lái)根據(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來(lái)包含菜單項(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菜單翻頁(yè)控制
 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)址", "書簽",
 "加入書簽", "分享頁(yè)面", "退出", "夜間模式", "刷新", "更多" };
 /** 菜單圖片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)橫屏", "筆選模式", "閱讀模式", "瀏覽模式", "快捷翻頁(yè)",
 "檢查更新", "檢查網(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:// 翻頁(yè)
  if (isMore) {
  menuGrid.setAdapter(getMenuAdapter(menu_name_array2,
  menu_image_array2));
  isMore = false;
  } else {// 首頁(yè)
  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菜單翻頁(yè)控制
 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)址", "書簽",
 "加入書簽", "分享頁(yè)面", "退出", "夜間模式", "刷新", "更多" };
 /** 菜單圖片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)橫屏", "筆選模式", "閱讀模式", "瀏覽模式", "快捷翻頁(yè)",
 "檢查更新", "檢查網(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:// 翻頁(yè)
  if (isMore) {
  menuGrid.setAdapter(getMenuAdapter(menu_name_array2,
  menu_image_array2));
  isMore = false;
  } else {// 首頁(yè)
  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í)有所幫助。

相關(guān)文章

最新評(píng)論