一、选项菜单(OptionsMenu)
在程序中直接通过代码创建选项菜单时需重写Activity的onCreateOptionsMenu和onOptionsItemSelected方法,其中onCreateOptionsMenu中用于创建菜单,onOptionsItemSelected用于监测菜单中哪一项被选中并在程序中做出响应。
public boolean onCreateOptionsMenu(Menu menu);通过调用Menu的addSubMenu方法返回一个SubMenu的子菜单,再调用SubMenu的add方法添加子菜单项。
public boolean onOptionsItemSelected(MenuItem item) ;通过调用MenuItem的getItemId方法来获得是哪个菜单项被选中,从而做出响应。
public boolean onOptionsItemSelected(MenuItem item);
package com.example.shiyanshi.usingmenu;
import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.textView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu fontSize=menu.addSubMenu("字体大小");
fontSize.add(0,R.dimen.FONT_10,0,"10号字体");
fontSize.add(0,R.dimen.FONT_12,0,"12号字体");
fontSize.add(0,R.dimen.FONT_14,0,"14号字体");
fontSize.add(0,R.dimen.FONT_16,0,"16号字体");
fontSize.add(0,R.dimen.FONT_18,0,"18号字体");
fontSize.add(0,R.dimen.FONT_20,0,"20号字体");
SubMenu ordinaryMenu=menu.addSubMenu("普通菜单项");
SubMenu fontColor=menu.addSubMenu("字体颜色");
fontColor.setHeaderTitle("选择字体颜色");
fontColor.add(0, R.color.FONT_RED, 0, "红色");
fontColor.add(0, R.color.FONT_GREEN, 0, "绿色");
fontColor.add(0, R.color.FONT_BLUE, 0, "蓝色");
SubMenu prog=menu.addSubMenu("启动程序");
prog.setHeaderTitle("选择你要启动的程序");
MenuItem item=prog.add("查看Swift");
item.setIntent(new Intent(MainActivity.this,AnotherActivity.class));
// MenuItem item1=prog.add("打开另外一个Activity");
// item1.setIntent(new Intent(MainActivity.this,ThirdActivity.class));
prog.add("打开另外一个Activity").setIntent(new Intent(MainActivity.this,ThirdActivity.class));
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Resources resources=MainActivity.this.getResources();
switch (item.getItemId()){
case R.dimen.FONT_10:
textView.setTextSize(resources.getDimension(R.dimen.FONT_10));
break;
case R.dimen.FONT_12:
textView.setTextSize(resources.getDimension(R.dimen.FONT_12));
break;
case R.dimen.FONT_14:
textView.setTextSize(resources.getDimension(R.dimen.FONT_14));
break;
case R.dimen.FONT_16:
textView.setTextSize(resources.getDimension(R.dimen.FONT_16));
break;
case R.dimen.FONT_18:
textView.setTextSize(resources.getDimension(R.dimen.FONT_18));
break;
case R.dimen.FONT_20:
textView.setTextSize(resources.getDimension(R.dimen.FONT_20));
break;
case R.color.FONT_RED:
textView.setTextColor(resources.getColor(R.color.FONT_RED));
break;
case R.color.FONT_GREEN:
textView.setTextColor(resources.getColor(R.color.FONT_GREEN));
break;
case R.color.FONT_BLUE:
textView.setTextColor(resources.getColor(R.color.FONT_BLUE));
break;
}
return super.onOptionsItemSelected(item);
}
}
二、上下文菜单(ContextMenu)
上下文菜单是指当长按某个控件时,所弹出的菜单,其创建步骤为:
1.使用registerForContextMenu为要创建上下文菜单的控件注册
2.在onCreateContextMenu方法中,调用ContextMenu的add方法添加菜单项
3.在onCreateContextMenu方法中,调用MenuItem的getItemId方法获得所选中的菜单,并进行响应。
package com.example.shiyanshi.contextmenu;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=(TextView)findViewById(R.id.txt);
registerForContextMenu(txt);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.add(0,R.color.BACK_RED,0,"红色");
menu.add(0,R.color.BACK_GREEN,0,"绿色");
menu.add(0,R.color.BACK_BLUE,0,"蓝色");
menu.setHeaderTitle("请选择背景颜色");
menu.setGroupCheckable(0,true,true);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.color.BACK_RED:
txt.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.BACK_RED));
break;
case R.color.BACK_GREEN:
txt.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.BACK_GREEN));
break;
case R.color.BACK_BLUE:
txt.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.BACK_BLUE));
break;
}
return super.onContextItemSelected(item);
}
}
三、通过xml文件创建菜单
通过xml文件创建菜单项时,只需在上述一二中onCreateOptionsMenu、onCreateContextMenu中创建MenuInflater(或调用getMenuInflater获得),并调用MenuInflater的inflater方法设置菜单项的资源文件。
其中在xml文件中item定义菜单项,其下可以有
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item> item用于定义菜单项,其下可以用menu和item结合定义子菜单
<menu> menu可以直接接子菜单项item,也可以接group分组后再接item,如下
<item></item>
<item></item>
</menu>
</item>
</menu>
<item>
<menu>
<group>
<item></item>
<item></item>
</group>
</menu>
</item>
1.MainActivity.java
package com.example.shiyanshi.usingxmlcreatemenu;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class XmlActivity extends AppCompatActivity {
private TextView txtOptions;
private TextView txtContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtOptions=(TextView)findViewById(R.id.txtOptions);
txtContext=(TextView)findViewById(R.id.txtContext);
registerForContextMenu(txtContext);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.options_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
switch (item.getItemId()){
case R.id.font10:
item.setChecked(true);
txtOptions.setTextSize(getResources().getDimension(R.dimen.FONT_10));
break;
case R.id.font12:
item.setChecked(true);
txtOptions.setTextSize(getResources().getDimension(R.dimen.FONT_12));
break;
case R.id.font14:
item.setChecked(true);
txtOptions.setTextSize(getResources().getDimension(R.dimen.FONT_14));
break;
case R.id.font16:
item.setChecked(true);
txtOptions.setTextSize(getResources().getDimension(R.dimen.FONT_16));
break;
case R.id.font18:
item.setChecked(true);
txtOptions.setTextSize(getResources().getDimension(R.dimen.FONT_18));
break;
case R.id.font20:
item.setChecked(true);
txtOptions.setTextSize(getResources().getDimension(R.dimen.FONT_20));
break;
case R.id.colorRed:
item.setChecked(true);
txtOptions.setTextColor(getResources().getColor(R.color.FONT_RED));
break;
case R.id.colorGreen:
item.setChecked(true);
txtOptions.setTextColor(getResources().getColor(R.color.FONT_GREEN));
break;
case R.id.colorBlue:
item.setChecked(true);
txtOptions.setTextColor(getResources().getColor(R.color.FONT_BLUE));
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.context_menu,menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.contextRed:
item.setChecked(true);
txtContext.setBackgroundColor(Color.RED);
break;
case R.id.contextGreen:
item.setChecked(true);
txtContext.setBackgroundColor(Color.GREEN);
break;
case R.id.contextBlue:
item.setChecked(true);
txtContext.setBackgroundColor(Color.BLUE);
break;
}
return super.onContextItemSelected(item);
}
}
2.options_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/fontSize"
android:title="@string/options_font_title">
<menu>
<group android:checkableBehavior="single" >
<item android:id="@+id/font10"
android:title="@string/font10"/>
<item android:id="@+id/font12"
android:title="@string/font12"/>
<item android:id="@+id/font14"
android:title="@string/font14"/>
<item android:id="@+id/font16"
android:title="@string/font16"/>
<item android:id="@+id/font18"
android:title="@string/font18"/>
<item android:id="@+id/font20"
android:title="@string/font20"/>
</group>
</menu>
</item>
<item android:id="@+id/fontColor"
android:title="@string/options_color_title">
<menu >
<group android:checkableBehavior="single">
<item android:id="@+id/colorRed"
android:title="@string/red"/>
<item android:id="@+id/colorGreen"
android:title="@string/green"/>
<item android:id="@+id/colorBlue"
android:title="@string/blue"/>
</group>
</menu>
</item>
</menu>
3.context_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group>
<item android:id="@+id/contextRed"
android:title="@string/red"/>
<item android:id="@+id/contextGreen"
android:title="@string/green"/>
<item android:id="@+id/contextBlue"
android:title="@string/blue"/>
</group>
</menu>
四、弹出菜单(PopupMenu)
弹出菜单是指某个组件被单击后,会在其上方或下方所弹出的菜单其创建步骤:
1.调用new PopupMenu(Context context, View anchor)方法创建弹出式菜单PopupMenu
2.调用MenuInflater的inflater方法将菜单资源添加到菜单
3.通过PopupMenu的setOnMenuItemClickListener方法监视菜单项的状态
4.调用PopupMenu的show方法显示菜单
package com.henry.popupmenu;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private PopupMenu popupMenu=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnPopup).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupMenu=new PopupMenu(MainActivity.this,v);
popupMenu.getMenuInflater().inflate(R.menu.popup_menu,popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.edit:
popupMenu.dismiss();
break;
default:
Toast.makeText(MainActivity.this, "您单击了"+ "【"+item.getTitle()+"】", Toast.LENGTH_SHORT).show();
}
return true;
}
});
popupMenu.show();
}
});
}
}