当前位置: 代码迷 >> Android >> Android自定义menu的兑现
  详细解决方案

Android自定义menu的兑现

热度:83   发布时间:2016-05-01 20:38:21.0
Android自定义menu的实现

?

public class MainAcvtivity extends Activity {   private int[] nameArr = new int[]{R.string.menu1,R.string.menu2,R.string.menu3,R.string.menu4};   private int[] imageArr = new int[]{R.drawable.menu1,R.drawable.menu2,R.drawable.menu3,R.drawable.menu4};   private  MyMenu myMenu;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        initMenu();    }    private void initMenu(){          myMenu = new MyMenu(this, nameArr, imageArr, 4) { 			 			@Override 			public void OnItemClick(int viewId) { 				switch (viewId) {				case R.string.menu1:					Toast.makeText(MainAcvtivity.this,getString(R.string.menu1), Toast.LENGTH_SHORT).show();					break;				case R.string.menu2:					Toast.makeText(MainAcvtivity.this,getString(R.string.menu2), Toast.LENGTH_SHORT).show();					break;				case R.string.menu3:					Toast.makeText(MainAcvtivity.this,getString(R.string.menu3), Toast.LENGTH_SHORT).show();					break;				case R.string.menu4:					Toast.makeText(MainAcvtivity.this,getString(R.string.menu4), Toast.LENGTH_SHORT).show();					break;				} 				myMenu.menuDismiss(); 			} 		};    }	@Override	public boolean onCreateOptionsMenu(Menu menu) {		menu.add("menu");		return super.onCreateOptionsMenu(menu);	}	@Override	public boolean onMenuOpened(int featureId, Menu menu) {		myMenu.showMenu(findViewById(R.id.main));		return false;	}public abstract class MyMenu{	private MainAcvtivity AbsActivity;	private int[] nameArr;	private int[] imageArr;	private int count;	private PopupWindow mPopupWindow;	private ViewFlipper mViewFlipper;	private List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();	public MyMenu(MainAcvtivity activity,int[] nameArr,int[] imageArr,int count){		this.AbsActivity = activity;    	this.nameArr = nameArr;    	this.imageArr = imageArr;    	this.count = count;    	 initMenu();	}    public void initMenu(){    	mViewFlipper = new ViewFlipper(AbsActivity);    	LinearLayout mLinearLayout = (LinearLayout) AbsActivity.getLayoutInflater().inflate(R.layout.menu_view, null);    	mViewFlipper.addView(mLinearLayout);    	mViewFlipper.setFlipInterval(1000000);    	GridView mGridView = (GridView) mLinearLayout.findViewById(R.id.menu_gridview);    	mGridView.setNumColumns(count);    	mPopupWindow = new PopupWindow(mViewFlipper, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);    	mPopupWindow.setBackgroundDrawable(new BitmapDrawable());    	mPopupWindow.setFocusable(true);    	mPopupWindow.update();    	for (int i = 0; i < nameArr.length; i++) {			Map<String, Object> map = new HashMap<String, Object>();			map.put("itemIamge", imageArr[i]);			map.put("itemText", AbsActivity.getString(nameArr[i]));			map.put("key", nameArr[i]);			data.add(map);		}    	BaseAdapter adapter = new SimpleAdapter(AbsActivity, data, R.layout.menu_item, new String[]{"itemIamge","itemText"},     			new int[]{R.id.menu_item_image,R.id.menu_item_text});    	mGridView.setAdapter(adapter);    	mGridView.setOnKeyListener(new OnKeyListener() {						@Override			public boolean onKey(View v, int keyCode, KeyEvent event) {				if(keyCode == KeyEvent.KEYCODE_MENU){					if(mPopupWindow != null){						mPopupWindow.dismiss();					}				}				return false;			}		});    	mGridView.setOnItemClickListener(new OnItemClickListener() {			@Override			public void onItemClick(AdapterView<?> parent, View view,					int position, long id) {				int key = Integer.parseInt( data.get(position).get("key").toString());				OnItemClick(key);			}		});    }    public void showMenu(View view){    	if(mPopupWindow != null){    		if(mPopupWindow.isShowing()){    			mPopupWindow.dismiss();    		}else{    			mPopupWindow.getContentView().setEnabled(true);    			mPopupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);    			mViewFlipper.startFlipping();    		}    	}    }    public void menuDismiss(){    	if(mPopupWindow !=null && mPopupWindow.isShowing()){    		mPopupWindow.dismiss();    	}    }	public abstract void OnItemClick(int viewId);}
  相关解决方案