public class ActionBarMechanicsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Normal item1");
menu.add("Normal item2");
MenuItem actionItem1 = menu.add("Action Button1");
MenuItem actionItem2 = menu.add("Action Button2");
MenuItem actionItem3 = menu.add("Action Button3");
actionItem1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
actionItem2.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
actionItem3.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
actionItem1.setIcon(android.R.drawable.alert_dark_frame);
actionItem2.setIcon(android.R.drawable.alert_dark_frame);
actionItem3.setIcon(android.R.drawable.ic_menu_share);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, "Selected Item: " + item.getTitle(),
Toast.LENGTH_SHORT).show();
return true;
}
}
这是3.0的一个功能模块,就是代替以前的应用中的title,在bar的左上方显示应用图标,右上方显示代替传统的menu的多个菜单按钮.