android 为activity添加optionMenu选项菜单

时间:2022-11-18 11:13:04
<menu xmlns:android="http://schemas.android.com/apk/res/android">

     <group android:checkableBehavior="single">
<item
android:id="@+id/contact_us"
android:orderInCategory="1"
android:showAsAction=""
android:title="联系我们"/>
<item android:title="关于我们"/>
</group> <group>
<item android:title="系统设置" android:orderInCategory="2"
android:icon="@drawable/ic_launcher"
android:id="@+id/system_setting"
>
<menu>
<item android:title="亮度设置" android:id="@+id/liangdu"/>
<item android:enabled="false" android:title="主题设置" android:id="@+id/theme"/>
</menu>
</item>
</group> </menu>

覆盖activity下的两个方法:

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.option_menu_main, menu);
return super.onCreateOptionsMenu(menu);
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.liangdu:
Toast.makeText(this, "点击了亮度", 1).show();
startActivity(new Intent(this, LiangduActivity.class));
break;
case R.id.contact_us:
Toast.makeText(this, "点击了关于我们", 1).show();
break;
}
return true;
}