今天老大分配的任务是完成系统菜单,刚开始我木呀,什么都不懂,还好提醒了我,是OptionsMenu,然后就上网找找,有点点小小的成就感
今天就两个内容,一个是系统自己自带的(也是我采用的) 二是自定义的,我也照着写了,但是出了问题,为啥我目前没明白,还是先贴来吧
一、系统的
item:每一个item对应一项菜单。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation ="vertical">
- <TextView android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="请点击Menu键显示选项菜单"
- android:id="@+id/TextView02"/>
- </LinearLayout>
OptionMenuDemo.java
- package com.option;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.Toast;
- public class OptionsMenuDemo extends Activity {
- /** Called when the activity is first created. */
- /* final static int ONE = Menu.FIRST;
- final static int TWO = Menu.FIRST + 1;*/
- // final static int THREE = Menu.FIRST+2;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // setContentView(R.layout.main);
- setContentView(R.layout.main1);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // TODO Auto-generated method stub
- menu.add(Menu.NONE, Menu.FIRST + 1, 1, "首页").setIcon(R.drawable.home);
- menu.add(Menu.NONE, Menu.FIRST + 2, 2, "新建").setIcon(R.drawable.sys_begin);
- menu.add(Menu.NONE, Menu.FIRST + 3, 3, "查询").setIcon(R.drawable.sys_query);
- menu.add(Menu.NONE, Menu.FIRST + 4, 4, "新闻").setIcon(R.drawable.sys_news);
- menu.add(Menu.NONE, Menu.FIRST + 5, 5, "退出").setIcon(R.drawable.menu_exit);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- super.onOptionsItemSelected(item);
- Intent intent = new Intent();
- switch (item.getItemId()) {
- case Menu.FIRST + 1:
- /*Toast.makeText(this, "首页", Toast.LENGTH_LONG).show();
- break;*/
- intent.setClass(OptionsMenuDemo.this, testActivity1.class);
- startActivity(intent);
- 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:
- AlertDialog alertDialog = new AlertDialog.Builder(this)
- .setTitle("退出系统")
- .setMessage("是否退出本系统?")
- .setPositiveButton("确定",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog,
- int which) {
- // TODO Auto-generated method stub
- System.exit(1);
- }
- })
- .setNegativeButton("取消",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog,
- int which) {
- // TODO Auto-generated method stub
- return;
- }
- }).create();
- alertDialog.show();
- break;
- }
- return false;
- }
- /*public void onOptionsMenuClosed(Menu menu) {
- Toast.makeText(this, "你关闭了选项菜单", Toast.LENGTH_SHORT).show();
- }*/
- public boolean onPrepareOptionMenu(Menu menu) {
- Toast.makeText(this, "选项菜单显示之前onPrepareOptionsMenu方法会被调用",
- Toast.LENGTH_LONG).show();
- return true;
- }
- }
效果:
单击退出出现
PS:关于项目,有很多的基类,所以,将这里的两个方法复制到基类中去即onCreateOptionsMenu
onOptionsItemSelected,如果有些方法在别的页面都一样,就写在基类中,否则,就写一个抽象方法,然后到具体的页面再去实现抽象方法,子类继承抽象类的方法,要实现抽象类中所有的抽象方法,这个明天尽量争取实现了~~后期也会有笔记~
一些解释:
android:icon:用于指定菜单显示的图标。
android:title:菜单的标题,显示在图标下面。
android:alphabeticShortcut:菜单选择的快捷键。
关于Menu的更多属性请查看SDK上的 Menu Resource 可以查看另一种方式:http://www.cnblogs.com/keyindex/articles/1813764.html
二、别人的自定义的
- 1.gridview_menu.xml (首先自定义菜单界面,我是GridView来包含菜单项,4列3行)
- <?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>
- 2.item_menu.xml菜单项的现实样式,一个图标和一个文字。
- <?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>
- <p>3<span style="font-family:宋体;">、定义,主类</span></p><p>private boolean isMore= false;// menu<span style="font-family:宋体;">菜单翻页控制</span>
- AlertDialog menuDialog;// menu<span style="font-family:宋体;">菜单</span><span style="font-family:Courier New;">Dialog</span>
- GridView menuGrid;
- View menuView;
- private finalint ITEM_SEARCH= 0;// 搜索
- private finalint ITEM_FILE_MANAGER= 1;// 文件管理
- private finalint ITEM_DOWN_MANAGER= 2;// 下载管理
- private finalint ITEM_FULLSCREEN= 3;// 全屏
- private finalint ITEM_MORE= 11;// 菜单
- /** <span style="font-family:宋体;">菜单图片 </span><span style="font-family:Courier New;">**/</span>
- 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 };
- /** <span style="font-family:宋体;">菜单文字 </span><span style="font-family:Courier New;">**/</span>
- String[] menu_name_array= {"<span style="font-family:宋体;">搜索</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">文件管理</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">下载管理</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">全屏</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">网址</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">书签</span><span style="font-family:Courier New;">"</span>,
- "<span style="font-family:宋体;">加入书签</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">分享页面</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">退出</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">夜间模式</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">刷新</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">更多</span><span style="font-family:Courier New;">"</span> };
- /** <span style="font-family:宋体;">菜单图片</span><span style="font-family:Courier New;">2 **/</span>
- 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 };
- /** <span style="font-family:宋体;">菜单文字</span><span style="font-family:Courier New;">2 **/</span>
- String[] menu_name_array2= {"<span style="font-family:宋体;">自动横屏</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">笔选模式</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">阅读模式</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">浏览模式</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">快捷翻页</span><span style="font-family:Courier New;">"</span>,
- "<span style="font-family:宋体;">检查更新</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">检查网络</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">定时刷新</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">设置</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">帮助</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">关于</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">返回</span><span style="font-family:Courier New;">"</span> };</p><p>4<span style="font-family:宋体;">、</span>如果第一次打开则设置视图,否则直接显示<span style="font-family:Times New Roman;">menuDialog</span><span style="font-family:宋体;">视图。</span></p><p>@Override
- public boolean onMenuOpened(int featureId, Menu menu) {
- if (menuDialog== null) {
- menuDialog= new AlertDialog.Builder(this).setView(menuView).show();
- }else {
- menuDialog.show();
- }
- return false;// 返 回为<span style="font-family:Courier New;">true </span>< span style="font-family:宋体;">则显示系统</span><span style="font- family:Courier New;">menu</span>
- }</p><p>1、getMenuAdapter方法 为菜单添加菜单项</p><p>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;
- }</p><p>6<span style="font-family:宋体;">、</span></p><p>Override
- public boolean onCreateOptionsMenu(Menu menu) {
- menu.add("menu");// 必须创建一项
- return super.onCreateOptionsMenu(menu);
- </p><p>7、onCreate<span style="font-family:宋体;">方法重写</span></p><p>@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);
- // 创建<span style="font-family:Courier New;">AlertDialog</span>
- menuDialog= new AlertDialog.Builder(this).create();
- menuDialog.setView(menuView);
- menuDialog.setOnKeyListener(new OnKeyListener() {
- public boolean onKey(DialogInterface dialog,int keyCode,
- KeyEventevent) {
- 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));
- /** <span style="font- family:宋体;">监听</span><span style="font-family:Courier New;"& gt;menu</span><span style="font-family:宋体;">选项 </span& gt;<span style="font-family:Courier New;">**/</span>
- 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();// 更新<span style="font-family:Courier New;">menu</span>
- menuGrid.setSelection(ITEM_MORE);
- break;
- }
- }
- });
- }</p>
参考的是http://www.cnblogs.com/keyindex/articles/1813764.html 是这个网站的