android- 菜单
选项菜单:menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:title="@string/font_size" android:icon="@drawable/daily10">
<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
android:id="@+id/plaint_item"
android:title="@string/plain_item"
/>
<item
android:title="@string/font_color"
android:icon="@drawable/daily5">
<menu>
<!-- 定义一组普通菜单项 -->
<group>
<!-- 定义三个菜单项 -->
<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
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 定义一组单选菜单项 -->
<group android:checkableBehavior="single">
<!-- 定义三个菜单项 -->
<item
android:id="@+id/red"
android:title="@string/red_back"
/>
<item
android:id="@+id/green"
android:title="@string/green_back"
/>
<item
android:id="@+id/blue"
android:title="@string/blue_back"
/>
</group>
</menu>
资源文件:strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MyMenu</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="font_size">字体大小</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>
<string name="plain_item">普通菜单项</string>
<string name="font_color">选择字体颜色</string>
<string name="red_title">红色字体</string>
<string name="green_title">绿色字体</string>
<string name="blue_title">蓝色字体</string>
<string name="red_back">红色背景</string>
<string name="green_back">绿色背景</string>
<string name="blue_back">蓝色背景</string>
</resources>
MainActivity.java
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.view.ContextMenu.ContextMenuInfo;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView) findViewById(R.id.textView);
//为文本框注册上下文菜单
registerForContextMenu(tv);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
/*
*
* */
MenuInflater inflator=new MenuInflater(this);
//装填对应的菜单,并添加到menu中
inflator.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
//创建上下文菜单时触发该方法
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
MenuInflater inflator=new MenuInflater(this);
//装填对应的菜单,并添加到menu中
inflator.inflate(R.menu.context, menu);
menu.setHeaderIcon(R.drawable.daily5);
menu.setHeaderTitle("选择背景颜色");
super.onCreateContextMenu(menu, v, menuInfo);
}
//上下文菜单中菜单项被单击时触发该方法
@Override
public boolean onContextItemSelected(MenuItem item) {
//勾选该菜单项
item.setCheckable(true);
switch(item.getItemId()){
case R.id.red:
item.setCheckable(true);
tv.setBackgroundColor(Color.RED);
break;
case R.id.green:
item.setCheckable(true);
tv.setBackgroundColor(Color.GREEN);
break;
case R.id.blue:
item.setCheckable(true);
tv.setBackgroundColor(Color.BLUE);
break;
}
return super.onContextItemSelected(item);
}
//菜单项被单击后回调方法
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.isCheckable()){
item.setCheckable(true);
}
switch(item.getItemId()){
case R.id.font_10:
tv.setTextSize(10*2);
break;
case R.id.font_12:
tv.setTextSize(12*2);
break;
case R.id.font_14:
tv.setTextSize(14*2);
break;
case R.id.font_16:
tv.setTextSize(16*2);
break;
case R.id.font_18:
tv.setTextSize(18*2);
break;
case R.id.red_font:
tv.setTextColor(Color.RED);
item.setCheckable(true);
break;
case R.id.green_font:
tv.setTextColor(Color.GREEN);
item.setCheckable(true);
break;
case R.id.blue_font:
tv.setTextColor(Color.BLUE);
item.setCheckable(true);
break;
case R.id.plaint_item:
Toast.makeText(this, "您单机了普通菜单项", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
}