为什么80%的码农都做不了架构师?>>>
这篇是基于:http://my.oschina.net/u/1773495/blog/306046 menu 菜单键写 的。
效果图片,点击Menu. 点击图片中的“更多”
接下来是:gridview_menu.xml
item_menu.xml:
主java程序:
package com.example.menutest;import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;public class MainActivity extends Activity {boolean isMore = false;private final int ITEM_NOTE = 0;private final int ITEM_MORE = 1;private View menuView;AlertDialog menuDialog;GridView menuGridView;int[] arrayImage1 = {R.drawable.menu_edit,R.drawable.menu_more};String [] arrayText1 ={ "书签","更多"};int[] arrayMore ={R.drawable.menu_return,R.drawable.menu_about};String [] arrayText2 ={"信息","返回"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);// 创建对话框menuDialog = new AlertDialog.Builder(this).create();Log.d("E","menuview");//获取view布局对象。第二参数root,表示是否给该布局外部再嵌套一层父布局,null为不要。//menuView = View.inflate(this, R.layout.gridview_menu, null);LayoutInflater layoutInflater = LayoutInflater.from(this); menuView = layoutInflater.inflate(R.layout.gridview_menu,null);// menuDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //将menuView(也就 是R.layout.gridview_menu) 加载到 menuDialogmenuDialog.setView(menuView);//给对话框 添加 按键事件。menuDialog.setOnKeyListener(new OnKeyListener() { @Overridepublic boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {if(keyCode==KeyEvent.KEYCODE_MENU){dialog.dismiss();//关闭对话框。//menuDialogbuilder.create().show();}return false;}});// 一定记得menuView加上。menuGridView = (GridView)menuView.findViewById(R.id.gridview);menuGridView.setAdapter(adapterDuilder(arrayImage1, arrayText1));menuGridView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView> arg0, View arg1, int arg2,long arg3) {switch (arg2) {case ITEM_NOTE:Toast.makeText(getApplicationContext(),"书签",Toast.LENGTH_LONG).show();break;case ITEM_MORE:if(isMore){menuGridView.setAdapter(adapterDuilder(arrayMore, arrayText2));isMore = false;}else{menuGridView.setAdapter(adapterDuilder(arrayImage1,arrayText1));isMore = true;}//更新 menu.menuGridView.invalidate();menuGridView.setSelection(ITEM_MORE);break;default:break;}} });}public SimpleAdapter adapterDuilder(int[]arrayImageName ,String[] arrayTextName) {ArrayList