国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

屬于自己的常見Android選項菜單樣式集合

2020-04-11 10:51:19
字體:
來源:轉載
供稿:網友

菜單是用戶界面中最常見的元素之一,使用非常頻繁,在Android中,菜單被分為如下三種,選項菜單(OptionsMenu)、上下文菜單(ContextMenu)和子菜單(SubMenu),今天這講是OptionsMenu 
一、概述

  •   public boolean onCreateOptionsMenu(Menu menu):使用此方法調用OptionsMenu 。
  •   public boolean onOptionsItemSelected(MenuItem item):選中菜單項后發生的動作。
  •   public void onOptionsMenuClosed(Menu menu):菜單關閉后發生的動作。
  •   public boolean onPrepareOptionsMenu(Menu menu):選項菜單顯示之前onPrepareOptionsMenu方法會被調用,你可以用此方法來根據打當時的情況調整菜單。
  •   public boolean onMenuOpened(int featureId, Menu menu):單打開后發生的動作。

二、默認樣式
默認樣式是在屏幕底部彈出一個菜單,這個菜單我們就叫他選項菜單OptionsMenu,一般情況下,選項菜單最多顯示2排每排3個菜單項,這些菜單項有文字有圖標,也被稱作Icon Menus,如果多于6項,從第六項開始會被隱藏,在第六項會出現一個More里,點擊More才出現第六項以及以后的菜單項,這些菜單項也被稱作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="請點擊 Menu鍵顯示選項菜單"  android:id="@+id/TextView02" /></LinearLayout>

2、重載onCreateOptionsMenu(Menu menu)方法
重載onCreateOptionsMenu(Menu menu)方法,并在此方法中添加菜單項,最后返回true,如果false,菜單則不會顯示。

@Override public boolean onCreateOptionsMenu(Menu menu) {  /*   *    * add()方法的四個參數,依次是:   *    * 1、組別,如果不分組的話就寫Menu.NONE,   *    * 2、Id,這個很重要,Android根據這個Id來確定不同的菜單   *    * 3、順序,那個菜單現在在前面由這個參數的大小決定   *    * 4、文本,菜單的顯示文本   */  menu.add(Menu.NONE, Menu.FIRST + 1, 5, "刪除").setIcon(  android.R.drawable.ic_menu_delete);  // setIcon()方法為菜單設置圖標,這里使用的是系統自帶的圖標,同學們留意一下,以  // android.R開頭的資源是系統提供的,我們自己提供的資源是以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, "詳細").setIcon(  android.R.drawable.ic_menu_info_details);  menu.add(Menu.NONE, Menu.FIRST + 6, 3, "發送").setIcon(  android.R.drawable.ic_menu_send);  return true; }

3、為菜單項注冊事件
使用onOptionsItemSelected(MenuItem item)方法為菜單項注冊事件

@Override public boolean onOptionsItemSelected(MenuItem item) {  switch (item.getItemId()) {  case Menu.FIRST + 1:   Toast.makeText(this, "刪除菜單被點擊了", Toast.LENGTH_LONG).show();   break;  case Menu.FIRST + 2:   Toast.makeText(this, "保存菜單被點擊了", Toast.LENGTH_LONG).show();   break;  case Menu.FIRST + 3:   Toast.makeText(this, "幫助菜單被點擊了", Toast.LENGTH_LONG).show();   break;  case Menu.FIRST + 4:   Toast.makeText(this, "添加菜單被點擊了", Toast.LENGTH_LONG).show();   break;  case Menu.FIRST + 5:   Toast.makeText(this, "詳細菜單被點擊了", Toast.LENGTH_LONG).show();   break;  case Menu.FIRST + 6:   Toast.makeText(this, "發送菜單被點擊了", 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()方法的四個參數,依次是:   *    * 1、組別,如果不分組的話就寫Menu.NONE,   *    * 2、Id,這個很重要,Android根據這個Id來確定不同的菜單   *    * 3、順序,那個菜單現在在前面由這個參數的大小決定   *    * 4、文本,菜單的顯示文本   */  menu.add(Menu.NONE, Menu.FIRST + 1, 5, "刪除").setIcon(  android.R.drawable.ic_menu_delete);  // setIcon()方法為菜單設置圖標,這里使用的是系統自帶的圖標,同學們留意一下,以  // android.R開頭的資源是系統提供的,我們自己提供的資源是以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, "詳細").setIcon(  android.R.drawable.ic_menu_info_details);  menu.add(Menu.NONE, Menu.FIRST + 6, 3, "發送").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, "刪除菜單被點擊了", Toast.LENGTH_LONG).show();   break;  case Menu.FIRST + 2:   Toast.makeText(this, "保存菜單被點擊了", Toast.LENGTH_LONG).show();   break;  case Menu.FIRST + 3:   Toast.makeText(this, "幫助菜單被點擊了", Toast.LENGTH_LONG).show();   break;  case Menu.FIRST + 4:   Toast.makeText(this, "添加菜單被點擊了", Toast.LENGTH_LONG).show();   break;  case Menu.FIRST + 5:   Toast.makeText(this, "詳細菜單被點擊了", Toast.LENGTH_LONG).show();   break;  case Menu.FIRST + 6:   Toast.makeText(this, "發送菜單被點擊了", Toast.LENGTH_LONG).show();   break;  }  return false; } @Override public void onOptionsMenuClosed(Menu menu) {  Toast.makeText(this, "選項菜單關閉了", Toast.LENGTH_LONG).show(); } @Override public boolean onPrepareOptionsMenu(Menu menu) {  Toast.makeText(this,    "選項菜單顯示之前onPrepareOptionsMenu方法會被調用,你可以用此方法來根據打當時的情況調整菜單",    Toast.LENGTH_LONG).show();  // 如果返回false,此方法就把用戶點擊menu的動作給消費了,onCreateOptionsMenu方法將不會被調用  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來包含菜單項,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="選項"></TextView></RelativeLayout>

菜單項的現實樣式,一個圖標和一個文字。

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 = { "搜索", "文件管理", "下載管理", "全屏", "網址", "書簽",   "加入書簽", "分享頁面", "退出", "夜間模式", "刷新", "更多" }; /** 菜單圖片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 = { "自動橫屏", "筆選模式", "閱讀模式", "瀏覽模式", "快捷翻頁",   "檢查更新", "檢查網絡", "定時刷新", "設置", "幫助", "關于", "返回" };@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 則顯示系統menu }

如果第一次打開則設置視圖,否則直接顯示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; }

為菜單添加菜單項。

@Override public boolean onCreateOptionsMenu(Menu menu) {  menu.add("menu");// 必須創建一項  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);  // 創建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)// 監聽按鍵     dialog.dismiss();    return false;   }  });  menuGrid = (GridView) menuView.findViewById(R.id.gridview);  menuGrid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array));  /** 監聽menu選項 **/  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 = { "搜索", "文件管理", "下載管理", "全屏", "網址", "書簽",   "加入書簽", "分享頁面", "退出", "夜間模式", "刷新", "更多" }; /** 菜單圖片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 = { "自動橫屏", "筆選模式", "閱讀模式", "瀏覽模式", "快捷翻頁",   "檢查更新", "檢查網絡", "定時刷新", "設置", "幫助", "關于", "返回" }; @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);  // 創建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)// 監聽按鍵     dialog.dismiss();    return false;   }  });  menuGrid = (GridView) menuView.findViewById(R.id.gridview);  menuGrid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array));  /** 監聽menu選項 **/  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");// 必須創建一項  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 則顯示系統menu } }

效果瀏覽:

以上就是本文的全部內容,希望對大家的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 广宗县| 福鼎市| 苏尼特左旗| 新蔡县| 榆树市| 柳江县| 聂拉木县| 铜鼓县| 平泉县| 错那县| 通河县| 古交市| 高密市| 庄河市| 正阳县| 岳西县| 龙南县| 唐山市| 全州县| 新昌县| 双鸭山市| 台东市| 白朗县| 南昌市| 克什克腾旗| 渝中区| 博爱县| 新源县| 和田市| 鹤山市| 临漳县| 晋城| 吉水县| 息烽县| 拉孜县| 南召县| 青龙| 清远市| 庆云县| 凌源市| 乡宁县|