Android里面也可用XML定义菜单资源,菜单分两种,一种是OptionMenu(点系统"Menu"键出来),一种是ContextMenu
下面定义两种菜单XML到res\menu文件夹下
my_menu为OptionMenu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android <item android:title="@string/font_size"
android:icon="@drawable/font">
<menu>
<!-- 定义一组单选菜单项 -->
<group android:checkableBehavior="single">
<!-- 定义多个菜单项 -->
<item android:id="@+id/font_10"
android:title="@string/font_10"/>
<item android:id="@+id/font_12"
android:title="@string/font_12"/>
<item android:id="@+id/font_14"
android:title="@string/font_14"/>
<item android:id="@+id/font_16"
android:title="@string/font_16"/>
<item android:id="@+id/font_18"
android:title="@string/font_18"/>
</group>
</menu>
</item>
<item>
</item>
<!-- 定义一个普通菜单项 -->
<item android:id="@+id/plain_item"
android:title="@string/plain_item">
</item>
<item android:id="@string/font_color"
android:title="@drawable/color">
<menu>
<!-- 定义一组普通的菜单项 -->
<group>
<!-- 定义3个菜单项 -->
<item android:id="@+id/red_font"
android:title="@string/red_title"/>
<item android:id="@+id/green_font"
android:title="@string/green_title"/>
<item android:id="@+id/blue_font"
android:title="@string/blue_title"/>
</group>
</menu>
</item>
</menu>
context.xml为ContextMenu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android=
<!-- 定义一组单选菜单项目 -->
<group android:checkableBehavior="single">
<!-- 定义三个菜单项 -->
<item android:id="@+id/red"
android:title="@string/red_title"
android:alphabeticShortcut="r"></item>
<item android:id="@+id/green"
android:title="@string/green_title"
android:alphabeticShortcut="g"></item>
<item android:id="@+id/blue"
android:title="@string/blue_title"
android:alphabeticShortcut="b"></item>
</group>
</menu>
定义两个res\value下的xml,用来指定颜色,字体
colors.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<color name="red">#ff0000</color>
<color name="green">#00ff00</color>
<color name="blue">#0000ff</color>
</resources>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, ContextMenuTest!</string>
<string name="app_name">上下文菜单</string>
<string name="red_title">红色</string>
<string name="blue_title">蓝色</string>
<string name="green_title">绿色</string>
<string name="font_size">字体大小</string>
<string name="font_color">字体颜色</string>
<string name="plain_item">普通菜单项</string>
<string name="font_10">10号字体</string>
<string name="font_12">12号字体</string>
<string name="font_14">14号字体</string>
<string name="font_16">16号字体</string>
<string name="font_18">18号字体</string>
</resources>
下面代码将实现两种菜单
package WangLi.Resource.MenuResTest;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MenuResTest extends Activity {
/** Called when the activity is first created. */
private TextView txt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt = (TextView)findViewById(R.id.txt);
//为文本框注册上下文菜单
registerForContextMenu(txt);
}
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflator = new MenuInflater(this);
//装填R.menu.my_menu对应的菜单,并添加到menu中
inflator.inflate(R.menu.my_menu, menu);
return super.onCreateOptionsMenu(menu);
}
//每次创建上下文菜单时都会触发该方法
public void onCreateContextMenu(ContextMenu menu,View source,
ContextMenu.ContextMenuInfo menuInfo)
{
MenuInflater inflator = new MenuInflater(this);
//装填R.menu.context对应的菜单,并添加到menu中
inflator.inflate(R.menu.context, menu);
menu.setHeaderIcon(R.drawable.tools);
menu.setHeaderTitle("请选择背景色");
}
//上下文菜单中菜单项被单击时触发该方法
public boolean onContextItemSelected(MenuItem mi)
{
switch(mi.getItemId())
{
case R.id.red:
mi.setChecked(true);
txt.setBackgroundColor(Color.RED);
break;
case R.id.green:
mi.setChecked(true);
txt.setBackgroundColor(Color.GREEN);
break;
case R.id.blue:
mi.setChecked(true);
txt.setBackgroundColor(Color.BLUE);
break;
}
return true;
}
//菜单项被单击后的回调方法
public boolean onOptionsItemSelected(MenuItem mi)
{
//判断单击的是哪个菜单项,并针对性地做出响应
switch(mi.getItemId())
{
case R.id.font_10:
txt.setTextSize(10*2);
break;
case R.id.font_12:
txt.setTextSize(12*2);
break;
case R.id.font_14:
txt.setTextSize(14*2);
break;
case R.id.font_16:
txt.setTextSize(16*2);
break;
case R.id.font_18:
txt.setTextSize(18*2);
break;
case R.id.red_font:
txt.setTextColor(Color.RED);
mi.setChecked(true);
break;
case R.id.green_font:
txt.setTextColor(Color.GREEN);
mi.setChecked(true);
break;
case R.id.blue_font:
txt.setTextColor(Color.BLUE);
mi.setChecked(true);
break;
case R.id.plain_item:
Toast toast = Toast.makeText(MenuResTest.this, "您单击了普通项菜单", Toast.LENGTH_SHORT);
toast.show();
break;
}
return true;
}
}
下面是程序运行效果图,刚启动时是一个TextView
点击后,在手机屏幕下方出现OptionMenu
点击字体大小或普通菜单项,出现子菜单
选择就可改变TextView的字体
如下点击"普通菜单项",会出现一个Toast提示,过几秒就会消失
那么怎么弹出ContextMenu呢,就是对着TextView长按就可以了
选择后,背景色就改变了