android的选项菜单的实现

来源:互联网 发布:c语言动态心形代码 编辑:程序博客网 时间:2024/05/17 07:57

        需要复写两个方法,OnCreateOptionsMenu方法和OnOptionsItemSelecte方法。前者使用自定义的XML文件,后者使用getItemId()方法来识别选中项。如果选项菜单少于6个,就会显示出来两行三列,否则自动加载到右下角的更多中去。菜单项可以设置title,icon等。

这是xml文件:

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android">     <item android:id="@+id/new_game"                 android:icon="@drawable/ic_launcher"                 android:title="@string/create_new"                 android:showAsAction="ifRoom"/>     <item android:id="@+id/open"                 android:icon="@drawable/ic_launcher"                 android:title="@string/open" /> </menu>
@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// TODO Auto-generated method stubswitch(item.getItemId()){case R.id.new_game:createNew();return true;case R.id.open:itemOpen();return true;default:return super.onOptionsItemSelected(item);}}



 

原创粉丝点击