参考《疯狂android讲义》2.10节 P174,参见归档project:XmlMenuDemo.zip
基本步骤如下:
1、定义布局文件
为简单显示原理,本布局只有一个EditText
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:id="@+id/et_hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="@string/hello_world" /> </RelativeLayout>
2、定义菜单资源文件
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 注意:string的第一个字母为小写,string.xml文件中也是!!! --> <item android:title="@string/menu_font_size"> <menu> <group android:checkableBehavior="single" > <item android:id="@+id/font_10" android:title="@string/font_10" /> <item android:id="@+id/font_20" android:title="@string/font_20"/> <item android:id="@+id/font_30" android:title="@string/font_30"/> <item android:id="@+id/font_40" android:title="@string/font_40"/> </group> </menu> </item> <item android:title="@string/plain_menu" android:id="@+id/menu_plain_menu" /> </menu>
(2)上下文菜单文件
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 注意:string的第一个字母为小写,string.xml文件中也是!!! --> <group android:checkableBehavior="single" > <item android:id="@+id/font_red" android:alphabeticShortcut="r" android:title="@string/red"/> <item android:id="@+id/font_green" android:alphabeticShortcut="r" android:title="@string/green"/> <item android:id="@+id/font_blue" android:alphabeticShortcut="r" android:title="@string/blue"/> </group> </menu>
3、重写onCreateOptionMenu及onCreateContextMenu
package com.ljh.xmlmenudemo; import android.os.Bundle; import android.app.Activity; import android.graphics.Color; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ContextMenu.ContextMenuInfo; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText etHelloWorld; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etHelloWorld = (EditText) findViewById(R.id.et_hello_world); registerForContextMenu(etHelloWorld); } @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; } @Override public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo) { getMenuInflater().inflate(R.menu.context,menu); super.onCreateContextMenu(menu,v,menuInfo); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { // 普通箱单被点击处所进行的操作。 case R.id.menu_plain_menu: Toast.makeText(this,"你单击了普通菜单",Toast.LENGTH_LONG).show(); break; // 为子菜单的子项定义被点击时所进行的操作。 case R.id.font_10: etHelloWorld.setTextSize(10); break; case R.id.font_20: etHelloWorld.setTextSize(20); break; case R.id.font_30: etHelloWorld.setTextSize(30); break; case R.id.font_40: etHelloWorld.setTextSize(40); break; } return super.onOptionsItemSelected(item); } @Override public boolean onContextItemSelected(MenuItem item) { item.setChecked(true); switch (item.getItemId()) { case R.id.font_red: item.setChecked(true); etHelloWorld.setBackgroundColor(Color.RED); break; case R.id.font_green: item.setChecked(true); etHelloWorld.setBackgroundColor(Color.GREEN); break; case R.id.font_blue: item.setChecked(true); etHelloWorld.setBackgroundColor(Color.BLUE); break; } return super.onContextItemSelected(item); } }