使用 xml资源文件定义菜单
menu文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:icon="@drawable/mia1"
android:title="@string/font_size">
<menu>
<!--定义一组单选菜单项-->
<group android:checkableBehavior="single">
<!--定义多个菜单项-->
<item
android:id="@+id/font10"
android:title="@string/font_10" />
<item
android:id="@+id/font12"
android:title="@string/font_12" />
<item
android:id="@+id/font14"
android:title="@string/font_14" />
<item
android:id="@+id/font16"
android:title="@string/font_16" />
<item
android:id="@+id/font18"
android:title="@string/font_18" />
</group>
</menu>
</item>
<!--定义一个普通菜单项-->
<item
android:id="@+id/plain_item"
android:title="@string/plain_item" />
<item
android:icon="@drawable/mia5"
android:title="@string/font_color">
<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>
<?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:alphabeticShortcut="r"
android:title="@string/red_title" />
<item
android:id="@+id/green"
android:alphabeticShortcut="g"
android:title="@string/green_title" />
<item
android:id="@+id/blue"
android:alphabeticShortcut="b"
android:title="@string/blue_title" />
</group>
</menu>
xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="xml定文件定义菜单"
android:id="@+id/textview001"/>
</LinearLayout>
java文件
public class MainActivity extends Activity {
private TextView tv;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
tv=findViewById(R.id.textview001);
//为文本框注册上下文菜单
registerForContextMenu(tv);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=new MenuInflater(this);
//装填R.menu.text_menu对应的菜单,并添加到menu中
inflater.inflate(R.menu.text_menu,menu);
return super.onCreateOptionsMenu(menu);
}
//創建上下文菜单时触发该方法
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater=new MenuInflater(this);
//装填R.menu.context,menu对应的菜单,并添加到menu中
inflater.inflate(R.menu.context,menu);
menu.setHeaderIcon(R.drawable.mia5);
menu.setHeaderTitle("请选择背景色");
super.onCreateContextMenu(menu, v, menuInfo);
}
//上下文菜单中菜单项被单击时触发该方法
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
//勾选该菜单项
item.setChecked(true);
switch (item.getItemId()){
case R.id.red:
item.setChecked(true);
tv.setBackgroundColor(Color.RED);
break;
case R.id.green:
item.setChecked(true);
tv.setBackgroundColor(Color.GREEN);
break;
case R.id.blue:
item.setChecked(true);
tv.setBackgroundColor(Color.BLUE);
break;
} return true;
}
//菜单项被单击后的回调方法
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.isCheckable()){
//勾选该菜单项
item.setChecked(true);
}
//判断单击的是那个才行项,并针对性的做出响应
switch (item.getItemId()){
case R.id.font10:
tv.setTextSize(10*2);
break;
case R.id.font12:
tv.setTextSize(12*2);
break;
case R.id.font14:
tv.setTextSize(14*2);
break;
case R.id.font16:
tv.setTextSize(16*2);
break;
case R.id.font18:
tv.setTextSize(18*2);
break;
case R.id.red_font:
tv.setTextColor(Color.RED);
item.setChecked(true);
break;
case R.id.green_font:
tv.setTextColor(Color.GREEN);
item.setChecked(true);
break;
case R.id.blue_font:
tv.setTextColor(Color.BLUE);
item.setChecked(true);
break;
case R.id.plain_item:
Toast toast=Toast.makeText(MainActivity.this,"您单击了普通菜单项",Toast.LENGTH_SHORT);
toast.show();
break;
}
return true;
}
}