想要让menue实现相应的功能首先要做的是写一个xml文件来对menu键的弹出页面来进行相应的设置
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);//获得所写的xml文件
return true;
}
@Override
选择功能的实现
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) { //每一个执行的项目都具有自己的专属的id,通过switch来进行找相同进行相应的设置
case R.id.action_settings:
intent =new Intent();
intent.setClass(MainActivity.this, Info.class);
startActivity(intent);
break;
default:break;
}
return super.onOptionsItemSelected(item);
}
对于菜单按钮的布局我们需要些在res目录下写上一个menu的布局文件然后进行书写item
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
//对每一个要执行的操作都要写一个item,然后为每一个item设置上独一无二的id方便在后面使用的时候对其进行调用
<item
android:id="@+id/action_settings"
android:orderInCategory="100"//设置所设置的item的位置
android:showAsAction="never"//android3.0版本之后出现的actionbar的兼用使用,写上never后其将不能够使用
android:title="@string/action_settings"//在菜单中所要显示的文字
/>
</menu>