摘要:Android - ActionBar Menu
要在app,顯示 ActionBar
則需要設定Theme 為 android:Theme.Holo 或繼續android:Theme.Holo的子類別
接著在Activity 如要有Up鍵則
要加入
ActionBar actionBar = this.getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
要加入Menu則要覆寫
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_actions, menu);
return super.onCreateOptionsMenu(menu);
}
menu_actions的xml如下
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="@+id/action_menu1"
android:orderInCategory="100"
android:showAsAction="withText"
android:title="Menu1"/>
<item
android:id="@+id/action_menu2"
android:orderInCategory="100"
android:showAsAction="withText"
android:title="Menu2"/>
</menu>
要執行按下Menu的動作,則覆寫
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_menu1:
return true;
case R.id.action_menu2:
return true;
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}