概述
关于选项菜单,请参考阅读《Android 从入门到精通》(27)——选项菜单,不再赘述
程序示例
完整工程:http://download.csdn.net/detail/sweetloveft/9439976
1.MainActivity.java
package com.sweetlover.activity; import com.sweetlover.optionmenu.R; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.root,menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onPrepareOptionsMenu(Menu menu) { // TODO Auto-generated method stub return super.onPrepareOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case R.id.delete: Toast.makeText(MainActivity.this,"删除菜单被点击了!",Toast.LENGTH_SHORT) .show(); break; case R.id.save: Toast.makeText(MainActivity.this,"保存菜单被点击了!",Toast.LENGTH_SHORT) .show(); break; case R.id.help: Toast.makeText(MainActivity.this,"帮助菜单被点击了!",Toast.LENGTH_SHORT) .show(); break; case R.id.add: Toast.makeText(MainActivity.this,"添加菜单被点击了!",Toast.LENGTH_SHORT) .show(); break; case R.id.detail: Toast.makeText(MainActivity.this,"详细菜单被点击了!",Toast.LENGTH_SHORT) .show(); break; case R.id.send: Toast.makeText(MainActivity.this,"发送菜单被点击了!",Toast.LENGTH_SHORT) .show(); break; } return super.onOptionsItemSelected(item); } @Override public void onOptionsMenuClosed(Menu menu) { // TODO Auto-generated method stub super.onOptionsMenuClosed(menu); Toast.makeText(MainActivity.this,"选项菜单关闭了!",Toast.LENGTH_SHORT) .show(); } }
2.activity_main.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" android:orientation="vertical" android:padding="30dp" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="10dp" android:textSize="20sp" android:text="@string/tittle" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="10dp" android:textSize="20sp" android:text="@string/note" /> </LinearLayout>
3.string.xml
<resources> <string name="app_name">OptionMenu</string> <string name="tittle">Option Menu Application</string> <string name="note">单击 Menu 键</string> <string name="oper1">删除</string> <string name="oper2">保存</string> <string name="oper3">帮助</string> <string name="oper4">添加</string> <string name="oper5">详细</string> <string name="oper6">发送</string> </resources>
4.res/menu/root.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <group android:id="@+id/group1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <item android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@drawable/miku" android:orderInCategory="1" android:title="@string/oper1"> </item> <item android:id="@+id/save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@drawable/miku" android:orderInCategory="2" android:title="@string/oper2"> </item> <item android:id="@+id/help" android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@drawable/miku" android:orderInCategory="3" android:title="@string/oper3"> </item> <item android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@drawable/miku" android:orderInCategory="4" android:title="@string/oper4"> </item> <item android:id="@+id/detail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@drawable/miku" android:orderInCategory="5" android:title="@string/oper5"> </item> <item android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@drawable/miku" android:orderInCategory="6" android:title="@string/oper6"> </item> </group> </menu>