<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group android:id="@+id/group1"><item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/app_name"/><item android:id="@+id/item1" android:orderInCategory="200" android:title="返回" android:alphabeticShortcut="B"></item><item android:id="@+id/item2" android:title="背景颜色">
<menu>
<item android:id="@+id/bggreen" android:title="绿色"/>
<item android:id="@+id/bgred" android:title="红色"/>
</menu>
</item><item android:id="@+id/item3" android:title="前景颜色">
<menu>
<item android:id="@+id/itemRed" android:title="红色"/>
<item android:id="@+id/itemBlue" android:title="蓝色"/>
<item android:id="@+id/itemGreen" android:title="绿色"/>
</menu>
</item></group>
</menu>
2.actionbar:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group android:id="@+id/group1">
<item android:id="@+id/newgame"
android:title="新游戏"
android:showAsAction="always">
</item>
<item android:id="@+id/help"
android:title="帮助"
android:showAsAction="always">
</item>
<item android:id="@+id/about"
android:title="关于"
android:showAsAction="always">
</item>
<item android:id="@+id/clock"
android:title="时钟"
android:actionLayout="@layout/clock"
android:showAsAction="always">
</item>
</group>
</menu>
3.contextmenu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group android:id="@+id/contextlogi">
<item android:id="@+id/copylogi" android:title="复制"></item>
<item android:id="@+id/paserlogi" android:title="粘贴"></item>
<item android:id="@+id/selectlogi" android:title="选择"></item>
</group>
</menu>
MainActivity:
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);
getMenuInflater().inflate(R.menu.actionbar, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
Color co = new Color();
Intent it =new Intent();
switch (item.getItemId()) {
case R.id.itemRed: {
tv.setTextColor(co.RED);
break;
}
case R.id.itemBlue:{
tv.setTextColor(co.BLUE);
break;
}
case R.id.itemGreen:{
tv.setTextColor(co.GREEN);
break;
}
case R.id.item1:{
it.setClass(MainActivity.this, LoginActivity.class);
startActivity(it);
finish();
}
case R.id.bgred:{
llayoutbg.setBackgroundColor(co.RED);
break;
}
case R.id.bggreen:{
llayoutbg.setBackgroundColor(co.GREEN);
break;
}
case R.id.help:{
it.setClass(this, HelpActivity.class);
startActivity(it);
}
case R.id.about:{
it.setClass(this, SecondActivity.class);
startActivity(it);
}
case R.id.newgame:{
it.setClass(this, GamesActivity.class);
startActivity(it);
}
default:{
}
}
return super.onOptionsItemSelected(item);
}
}
GameActivity:
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.actionbar, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case android.R.id.home:{
finish();
}
}
return super.onOptionsItemSelected(item);
}
}
SecondActivity:
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.actionbar, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
Intent intent=getIntent();
switch(item.getItemId()){
case android.R.id.home:
// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// startActivity(intent);
finish();
break;
case R.id.help :
intent.setClass(this, HelpActivity.class);
startActivity(intent);
finish();
break;
case R.id.newgame :
intent.setClass(this, GamesActivity.class);
startActivity(intent);
finish();
break;
default :{
break;
}
}
return super.onOptionsItemSelected(item);
}
}