Action Bar(工具栏)

1、On API level 11 or higher

The action baris included in all activities that use the Theme.Holo theme (or one of its descendants),which is the default theme when either the targetSdkVersion or minSdkVersionattribute is set to "11" or higher. If you don't want the action barfor an activity, set the activity theme to Theme.Holo.NoActionBar.

2、Removing the action bar

You can hide theaction bar at runtime by calling hide(). For example:

ActionBar actionBar = getActionBar();
actionBar.hide();

3、Adding Action Items


Figure1 : Action bar with three action buttons and the overflow button.

The action barprovides users access to the most important action items relating to the app'scurrent context. Those that appear directly in the action bar with an iconand/or text are known as actionbuttons. Actions that can't fit in the action bar or aren't importantenough are hidden in the actionoverflow.

When youractivity starts, the system populates the action items by calling youractivity's onCreateOptionsMenu()method. Use this method to inflate a menu resource that defines all the actionitems.

@Override
public boolean onCreateOptionsMenu(Menumenu) {    
   // Inflate the menu items for use in the action bar    
   MenuInflater inflater = getMenuInflater();    
   inflater.inflate(R.menu.main_activity_actions, menu);    
   return super.onCreateOptionsMenu(menu);
}
To request thatan item appear directly in the action bar as an action button, include showAsAction="ifRoom"in the <item> tag. If there's not enough room for the item in the actionbar, it will appear in the action overflow.

If your menuitem supplies both a title and an icon—with the title and icon attributes—thenthe action item shows only the icon by default. If you want to display the texttitle, add "withText"to the showAsAction attribute. For example:

<?xml version="1.0"encoding="utf-8"?>
<menuxmlns:android="http://schemas.android.com/apk/res/android">
   <item android:id="@+id/menu_save"
         android:icon="@drawable/ic_menu_save"
         android:title="@string/menu_save"
         android:showAsAction="ifRoom|withText" />
</menu>
Note: The "withText" value is a hint to the action bar that thetext title should appear. The action bar will show the title when possible, butmight not if an icon is available and the action bar is constrained for space.

4、Navigating Up with the App Icon


Figure 2 : The Up button in Gmail.

Enabling the app icon as an Up button allows theuser to navigate your app based on the hierarchical relationships betweenscreens. For instance, if screen A displays a list of items, and selecting anitem leads to screen B, then screen B should include the Up button, whichreturns to screen A.

Note: Up navigation isdistinct from the back navigation provided by the system Back button. The Backbutton is used to navigate in reverse chronological order through the historyof screens the user has recently worked with. It is generally based on thetemporal relationships between screens, rather than the app's hierarchystructure (which is the basis for up navigation).

To enable the appicon as an Up button, call setDisplayHomeAsUpEnabled(true). For example:

@Override
protected void onCreate(BundlesavedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
   ActionBar actionBar = getActionBar();
   actionBar.setDisplayHomeAsUpEnabled(true);
   ...
}
当用户触摸这个图标时,系统会调用Activity带有android.R.id.home ID的onOptionsItemSelected()方法。在这个响应中,你既可以启动主Activity,也可以返回你的应用程序结构化层次中用户上一步操作的界面。

例如,下例的onOptionsItemSelected()方法实现了返回应用程序的主Activity的操作:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()) {
       case android.R.id.home:
           // app icon in action bar clicked; go home
           Intent intent = new Intent(this, HomeActivity.class);
           intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
           startActivity(intent);
           return true;
       default:
           return super.onOptionsItemSelected(item);
    }
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值