<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="white-space:pre"> </span>今天给大家带来的Menu的多种使用方式</span>
在创建一个应用的时候就会看到在MainActivity中会有这个类方法
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
获取加载器,加载你写的Menu.xml,返回ture;
这是最基本的使用方式,然而在开发根本不能满足我们的需求,今天给大家带来常用的几种方式
最常用的方式就是在menu文件夹中写入自己想要的布局
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Options First"/>
<item
android:id="@+id/action_settings2"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Optons Second"/>
</menu>
这是一种以xml的方式
除了这种方法我们还可以动态的添加Menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//xml 形式添加Menu
// getMenuInflater().inflate(R.menu.main, menu);
//动态添加Options
MenuItem item = menu.add(1, 10, 1, "Options First");
item.setTitle("Options First 2");
menu.add(1, 11, 1, "Options Second");
menu.add(1, 12, 1, "Options Three");
menu.add(1, 13, 1, "Options Four");
return true;
}
其中使用menu里面的add()方法即可添加,其中第一个参数为MenuGroupId,第二个参数为id, 第三个参数为排序方式, 第四个参数为Menu的名称
MenuItem item = menu.add(1, 10, 1, "Options First");
item.setTitle("Options First 2");
获取到Menuitem,还可以设置标题,在 sdk > 11的时候可以添加图标但是现在不行了
接下来设置menu点击后相应时间,其中需要在里面重写onOptionsItemSelected(MenuItem item)方法
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
/* switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(getApplicationContext(), "Onclick Options First", Toast.LENGTH_LONG).show();
break;
case R.id.action_settings2:
Toast.makeText(getApplicationContext(), "Onclick Options Second", Toast.LENGTH_LONG).show();
break;
default:
break;
}*/
switch (item.getItemId()) {
case 10:
// item.setIntent(arg0); 跳转到某界面上去
Toast.makeText(getApplicationContext(), "Onclick Options First", Toast.LENGTH_LONG).show();
break;
case 11:
Toast.makeText(getApplicationContext(), "Onclick Options Second", Toast.LENGTH_LONG).show();
break;
case 12:
Toast.makeText(getApplicationContext(), "Onclick Options Three", Toast.LENGTH_LONG).show();
break;
case 13:
Toast.makeText(getApplicationContext(), "Onclick Options Four", Toast.LENGTH_LONG).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
我们在里面可以实现页面的跳转,在里面可以获取到MenuItem的setItemid() or setIntent();
第二种方式设置 : ContextMenu
相信大家都是用过文件管理器,长按的状态下,会触发另外一种Menu,而这种Menu就是ContextMenu
首先我们需要在View注册registerForContextMenu()
然后添加上下文菜单内容onCreateContextMenu()
-- 通过加载的方式添加menu,xml
-- 通过menu动态的添加菜单项
设置菜单后点击菜单的响应事件onContextItemSelected();
private void showListview() {
// TODO Auto-generated method stub
ListView listView = (ListView) findViewById(R.id.listviewid);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_list_item_1,
getdata());
listView.setAdapter(adapter);
// 注册上下文菜单
this.registerForContextMenu(listView);
}
private ArrayList<String> getdata() {
// TODO Auto-generated method stub
ArrayList<String> arrayList = new ArrayList<String>();
for (int i = 0; i < 5; ++i) {
arrayList.add("文件" + (i + 1));
}
return arrayList;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
menu.setHeaderTitle("Menu");
menu.setHeaderIcon(R.drawable.ic_launcher);
// //静态注册
// MenuInflater inflater = getMenuInflater();
// inflater.inflate(R.menu.main, menu);
menu.add(1, 1, 1, "copy");
menu.add(1, 2, 1, "paste");
menu.add(1, 3, 1, "rename");
menu.add(1, 4, 1, "delete");
super.onCreateContextMenu(menu, v, menuInfo);
}
在Menu.xml添加Menu菜单
MenuInflater inflater = getMenuInflater();
nflater.inflate(R.menu.main, menu);
设置点击Menu事件:
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case 1:
Toast.makeText(getApplicationContext(), "Onclick a copy",
Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(getApplicationContext(), "Onclick a paste",
Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(getApplicationContext(), "Onclick a rename",
Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(getApplicationContext(), "Onclick a delete",
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onContextItemSelected(item);
}
第三种方法 : SubMenu
子项菜单
onCreateOptionsMenu() 重写此方法,
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.main, menu);
// add second SunMenu
SubMenu file = menu.addSubMenu("File");
SubMenu edit = menu.addSubMenu("Edit");
file.add(1, 1, 1, "Copy");
file.add(1, 2, 1, "Paste");
file.add(1, 3, 1, "Rename");
file.add(1, 4, 1, "Delete");
file.setHeaderIcon(R.drawable.ic_launcher);
file.setHeaderTitle("File Operation");
edit.add(2, 1, 1, "EditTitle");
edit.add(2, 2, 1, "EditContext");
edit.setHeaderIcon(R.drawable.ic_launcher);
edit.setHeaderTitle("Edit Operation");
return true;
}
使用menu.subMenu(“menu名称”),从而获取subMenu方法,使用SubMenu.add在添加它的子Menu,
file.setHeaderIcon(R.drawable.ic_launcher);
file.setHeaderTitle("File Operation");
设置它的标题和图片
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if (item.getGroupId() == 1) {
switch (item.getItemId()) {
case 1:
Toast.makeText(MainActivity.this, "You Onclick Copy",
Toast.LENGTH_LONG).show();
break;
case 2:
Toast.makeText(MainActivity.this, "You Onclick Paste",
Toast.LENGTH_LONG).show();
break;
case 3:
Toast.makeText(MainActivity.this, "You Onclick Rename",
Toast.LENGTH_LONG).show();
break;
case 4:
Toast.makeText(MainActivity.this, "You Onclick Delete",
Toast.LENGTH_LONG).show();
break;
}
} else if (item.getGroupId() == 2) {
switch (item.getItemId()) {
case 1:
Toast.makeText(MainActivity.this, "You Onclick EditTitle",
Toast.LENGTH_LONG).show();
break;
case 2:
Toast.makeText(MainActivity.this, "You Onclick EditContext",
Toast.LENGTH_LONG).show();
break;
}
}
return super.onContextItemSelected(item);
}
看到这里调用了getGroupId()方法,这个方法获取到前面所说的MenuGroupId,然后在对子项菜单进行id判断
我们需要在AndroidManifest.xml里面添加上,从而隐藏掉布局右上角的三个点
android:theme="@android:style/Theme.Light.NoTitleBar"
今天为大家介绍了这几种的Menu的常用方式,希望大家多多支持我,谢谢