使用ActionBar设置菜单例子
/res/menu/activity_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/menu_settings" android:orderInCategory="100" android:showAsAction="always" android:title="@string/menu_settings" /> <item android:id="@+id/menu_settingss" android:orderInCategory="100" android:showAsAction="always" android:icon="@android:drawable/ic_menu_sort_by_size"> <menu> <item android:id="@+id/action_sort_size" android:icon="@android:drawable/ic_menu_sort_by_size" android:title="action_bar_sort_size"/> <item android:id="@+id/action_sort_alpha" android:icon="@android:drawable/ic_menu_sort_alphabetically" android:title="action_bar_sort_alpha"/> </menu> </item> </menu>
MainActivity.java
package com.example.actionbartab;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuInflater;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.activity_main, menu); return true; }}