一、前言
转载请标明出处:http://blog.csdn.net/wlwlwlwl015/article/details/42170771
本篇blog将记录一下Android中菜单的使用方法,虽然在新版本中推荐使用ActionBar去替代菜单,但我认为菜单依然是挺不错的一种UI交互组件,比ActionBar好点一些,对于手指不灵活的人比起来ActionBar右上角的那个方块小按钮,或许ta会更喜欢Menu呢,废话不多说,下面就具体介绍一下Android中关于Menu的使用方法。
二、创建第一个菜单
菜单有两种创建方式,分别是:
这里我想也不必多说,肯定应该选择第二种形式,在Activity中写代码去创建菜单必然导致程序代码过于臃肿,而且也不易查看和维护,而第二种方法既容易观察菜单的结构,也降低了耦合性,下面是官方给出的将“Using a menu resource”作为最佳实践的理由,简单看一下:
所以我这里就不浪费篇幅去记录“通过代码去创建菜单”了,主要记录一下如何通过xml文件定义菜单资源以及如何在Activity中引用并设置以及使用菜单。
菜单的资源文件通常应该放在/res/menu目录下,菜单资源的根元素通常是
可以看到
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case R.id.item_1:
Toast.makeText(this, "item_1 has been clicked!", Toast.LENGTH_SHORT)
.show();
;
break;
case R.id.item_2:
Toast.makeText(this, "item_2 has been clicked!", Toast.LENGTH_SHORT)
.show();
break;
case R.id.item_3:
Toast.makeText(this, "item_3 has been clicked!", Toast.LENGTH_SHORT)
.show();
break;
case R.id.item_4:
Toast.makeText(this, "item_4 has been clicked!", Toast.LENGTH_SHORT)
.show();
break;
case R.id.item_5:
Toast.makeText(this, "item_5 has been clicked!", Toast.LENGTH_SHORT)
.show();
break;
case R.id.item_6:
Toast.makeText(this, "item_6 has been clicked!", Toast.LENGTH_SHORT)
.show();
break;
}
return true;
}
最后看一下运行效果:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = (ListView) findViewById(R.id.lv_test);
registerForContextMenu(lv); //为上下文菜单注册一个ListView
}
下面看一下接下来的需要做的事情,文档中也已经说的很清楚了:
package com.wl.contextmenudemo;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import com.wl.model.BaiduItem;
import com.wl.view.MyListView;
public class MainActivity extends Activity {
private BaiduItem baiduItem;
private Context context;
private LayoutInflater inflater;
private ScrollView sv;
private MyListView mlv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
inflater = LayoutInflater.from(context);
sv = (ScrollView) findViewById(R.id.sv_4lv);
sv.smoothScrollTo(0, 0);
MyListView lv = (MyListView) findViewById(R.id.lv_test);
baiduItem = new BaiduItem();
lv.setAdapter(new Myadapter());
registerForContextMenu(lv); // 为上下文菜单注册一个ListView
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.my_menu_2, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(context, "新窗口打开!", Toast.LENGTH_SHORT).show();
break;
case R.id.item2:
Toast.makeText(context, "后台打开!", Toast.LENGTH_SHORT).show();
break;
case R.id.item3:
Toast.makeText(context, "选择文本!", Toast.LENGTH_SHORT).show();
break;
case R.id.item4:
Toast.makeText(context, "复制链接地址!", Toast.LENGTH_SHORT).show();
break;
case R.id.item5:
Toast.makeText(context, "下载链接!", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
return super.onCreateOptionsMenu(menu);
}
class Myadapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return baiduItem.items.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return baiduItem.items.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.my_item_1, null);
TextView title = (TextView) v.findViewById(R.id.tv_title);
title.setText(baiduItem.items.get(position).getTitle());
TextView content = (TextView) v.findViewById(R.id.tv_content);
content.setText(baiduItem.items.get(position).getContent());
TextView url = (TextView) v.findViewById(R.id.tv_url);
url.setText(baiduItem.items.get(position).getUrlAndDate());
return v;
}
};
}
package com.wl.menusdemo;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.PopupMenu;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private ImageButton ibtnAdd;
private ImageButton ibtnSearch;
private PopupMenu popMenu;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
ibtnAdd = (ImageButton) findViewById(R.id.ibtn_add);
ibtnAdd.setOnClickListener(this);
ibtnSearch = (ImageButton) findViewById(R.id.ibtn_search);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == ibtnAdd) {
popMenu = new PopupMenu(context, ibtnAdd);
getMenuInflater().inflate(R.menu.pop_menu, popMenu.getMenu());
popMenu.show();
popMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.item_chart:
Toast.makeText(context, "发起群聊!", Toast.LENGTH_SHORT)
.show();
break;
case R.id.item_add_friend:
Toast.makeText(context, "添加朋友!", Toast.LENGTH_SHORT)
.show();
break;
case R.id.item_sao:
Toast.makeText(context, "扫一扫!", Toast.LENGTH_SHORT)
.show();
break;
case R.id.item_idea:
Toast.makeText(context, "意见反馈!", Toast.LENGTH_SHORT)
.show();
break;
}
return false;
}
});
}
}
}
package com.wl.menusdemo;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;
public class SecondActivity extends Activity {
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
context = this;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_MENU) {
Toast.makeText(context, "准备好,要弹出菜单了!", Toast.LENGTH_LONG).show();
}
return true;
}
}
package com.wl.menusdemo;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.Toast;
public class SecondActivity extends Activity implements OnClickListener {
private Context context;
private PopupWindow popupWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
context = this;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_MENU) {
showMenu();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void showMenu() {
LayoutInflater inflater = getLayoutInflater();
View menuView = inflater.inflate(R.layout.pop_menu, null);
Button button1 = (Button) menuView.findViewById(R.id.btn_ver_update);
Button button2 = (Button) menuView.findViewById(R.id.btn_help);
Button button3 = (Button) menuView.findViewById(R.id.btn_exit_qq);
Button button4 = (Button) menuView.findViewById(R.id.btn_undo);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
// 设置PopupWindow宽高
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
double screenWidth = metrics.widthPixels * 0.95;
popupWindow = new PopupWindow(menuView, (int) screenWidth,
LayoutParams.WRAP_CONTENT);
// 想要让PopupWindow中的控件能够使用,就必须设置PopupWindow为focusable
popupWindow.setFocusable(true);
// 想做到在你点击PopupWindow以外的区域时候让PopupWindow消失就做如下两步操作
// 1:设置setOutsideTouchable为ture,这个很容易理解吧,跟AlertDialog一样的
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(getResources().getDrawable(
android.R.color.transparent));
popupWindow.setAnimationStyle(R.style.AnimationPreview);
popupWindow.setOnDismissListener(onDismissListener);
popupWindow.showAtLocation(getWindow().getDecorView(),
android.view.Gravity.BOTTOM, 0, 0);
// 设置父窗口透明度
WindowManager.LayoutParams wlp = getWindow().getAttributes();
wlp.alpha = 0.5f;
getWindow().setAttributes(wlp);
}
private OnDismissListener onDismissListener = new OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams wlp = getWindow().getAttributes();
wlp.alpha = 1.0f;
getWindow().setAttributes(wlp);
}
};
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (popupWindow != null && popupWindow.isShowing())
popupWindow.dismiss();
switch (v.getId()) {
case R.id.btn_ver_update:
Toast.makeText(context, "版本更新!", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_help:
Toast.makeText(context, "帮助与反馈!", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_exit_qq:
Toast.makeText(context, "退出QQ!", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_undo:
Toast.makeText(context, "取消!", Toast.LENGTH_SHORT).show();
break;
}
}
}