Adding Action Items


  The action bar provides users access to the most important action items relating to the app's current context. Those that appear directly in the action bar with an icon and/or text are known as action buttons. Actions that can't fit in the action bar or aren't important enough are hidden in the action overflow. The user can reveal a list of the other actions by pressing the overflow button on the right side (or the device Menu button, if available).

  When your activity starts, the system populates the action items by calling your activity's onCreateOptionsMenu() method. Use this method to inflate a menu resource that defines all the action items. For example, here's a menu resource defining a couple of menu items:

res/menu/main_activity_actions.xml 操作项资源文件如下:

 <menu xmlns:android="http://schemas.android.com/apk/res/android" >
     <item android:id="@+id/action_search"
           android:icon="@drawable/ic_action_search"
           android:title="@string/action_search"/>
     <item android:id="@+id/action_compose"
           android:icon="@drawable/ic_action_compose"
           android:title="@string/action_compose" />
 </menu>

  Then in your activity's onCreateOptionsMenu() method, inflate the menu resource into the given Menu to add each item to the action bar:

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
     // 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);
 }
 在 activity或fragment的 onCreateOptionsMenu() 函数中加载操作项资源文件.

  To request that an item appear directly in the action bar as an action button, include showAsAction="ifRoom"in the <item> tag. For example:

 <menu xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
     <item android:id="@+id/action_search"
           android:icon="@drawable/ic_action_search"
           android:title="@string/action_search"
           yourapp:showAsAction="ifRoom"  />
     ...
 </menu>

  If there's not enough room for the item in the action bar, it will appear in the action overflow.

Using XML attributes from the support library

  Notice that the showAsAction attribute above uses a custom namespace defined in the <menu> tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.

  If your menu item supplies both a title and an icon—with the title and icon attributes—then the action item shows only the icon by default. If you want to display the text title, add "withText" to the showAsActionattribute. For example:

<item yourapp:showAsAction="ifRoom|withText" ... />
  标题与图标同时存在时默认只显示图标

  Note: The "withText" value is a hint to the action bar that the text title should appear. The action bar will show the title when possible, but might not if an icon is available and the action bar is constrained for space.

  You should always define the title for each item even if you don't declare that the title appear with the action item, for the following reasons:

  • If there's not enough room in the action bar for the action item, the menu item appears in the overflow where only the title appears.
  • Screen readers for sight-impaired users read the menu item's title.
  • If the action item appears with only the icon, a user can long-press the item to reveal a tool-tip that displays the action title.

  The icon is optional, but recommended. For icon design recommendations, see the Iconography design guide. You can also download a set of standard action bar icons (such as for Search or Discard) from the Downloadspage.

  You can also use "always" to declare that an item always appear as an action button. However, you should not force an item to appear in the action bar this way. Doing so can create layout problems on devices with a narrow screen. It's best to instead use "ifRoom" to request that an item appear in the action bar, but allow the system to move it into the overflow when there's not enough room. However, it might be necessary to use this value if the item includes an action view that cannot be collapsed and must always be visible to provide access to a critical feature.

  ifRoom:如果空间足就显示,否则在溢出菜单里,  withText:显示操作项的标题,  always:问题显示,  never:只在溢出菜单里显示.它们可以用 |  

Handling clicks on action items 处理操作项事件

  When the user presses an action, the system calls your activity's onOptionsItemSelected() method. Using the MenuItem passed to this method, you can identify the action by calling getItemId(). This returns the unique ID provided by the <item> tag's id attribute so you can perform the appropriate action. For example:

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
     // Handle presses on the action bar items
     switch (item.getItemId()) {
         case R.id.action_search:
             openSearch();
             return true;
         case R.id.action_compose:
             composeMessage();
             return true;
         default:
             return super.onOptionsItemSelected(item);
     }
 }

  Note: If you inflate menu items from a fragment, via the Fragment class's onCreateOptionsMenu() callback, the system calls onOptionsItemSelected() for that fragment when the user selects one of those items. However, the activity gets a chance to handle the event first, so the system first calls onOptionsItemSelected() on the activity, before calling the same callback for the fragment. To ensure that any fragments in the activity also have a chance to handle the callback, always pass the call to the superclass as the default behavior instead of returning false when you do not handle the item.

  注意: 如果是在fragment的处理操作项,那么fragment宿主会先于它收到事件,虽然在宿主中返回false事件也会传到fragment中,但最好的方式是用 

      return super.onOptionsItemSelected(item);


ActionBar官方教程(4)给ActionBar添加操作项及它们的事件处理的更多相关文章

  1. ActionBar官方教程(8)ShareActionProvider与自定义操作项提供器

    Adding an Action Provider Similar to an action view, an action provider replaces an action button wi ...

  2. ActionBar官方教程(11)自定义ActionBar的样式(含重要的样式属性表及练习示例)

    Styling the Action Bar If you want to implement a visual design that represents your app's brand, th ...

  3. Asp.Net MVC4.0 官方教程 入门指南之四--添加一个模型

    Asp.Net MVC4.0 官方教程 入门指南之四--添加一个模型 在这一节中,你将添加用于管理数据库中电影的类.这些类是ASP.NET MVC应用程序的模型部分. 你将使用.NET Framewo ...

  4. Asp.Net MVC4.0 官方教程 入门指南之三--添加一个视图

    Asp.Net MVC4.0 官方教程 入门指南之三--添加一个视图 在本节中,您需要修改HelloWorldController类,从而使用视图模板文件,干净优雅的封装生成返回到客户端浏览器HTML ...

  5. ActionBar官方教程(7)自定义操作项的view,如何得到它及处理它的事件

    Adding an Action View An action view is a widget that appears in the action bar as a substitute for ...

  6. ActionBar官方教程(9)ActionBar的顶部tab模式(注意,已经被弃用)

    This interface is deprecated.Action bar navigation modes are deprecated and not supported by inline ...

  7. ActionBar官方教程(10)ActionBar的下拉列表模式

    Adding Drop-down Navigation As another mode of navigation (or filtering) for your activity, the acti ...

  8. ActionBar官方教程(6)把图标变成一个返回到上级的按钮,同一个app间,不同app间,不同fragment间

    Navigating Up with the App Icon Enabling the app icon as an Up button allows the user to navigate yo ...

  9. ActionBar官方教程(5)ActionBar的分裂模式(底部tab样式),隐藏标题,隐藏图标

    Using split action bar Split action bar provides a separate bar at the bottom of the screen to displ ...

随机推荐

  1. javascript 事件 第23节

    <html> <head> <title>DOM对象</title> <style type="text/css"> t ...

  2. java异常处理练习

    异常的练习: 老师用电脑上课. 开始思考上课中出现的问题. 比如问题是    电脑蓝屏.    电脑冒烟. 要对问题进行描述,封装成对象. 可是当冒烟发生后,出现讲课进度无法继续. 出现了讲师的问题: ...

  3. Codevs 1001 舒适的路线 2006年 NOIP全国联赛提高组

    1001 舒适的路线 2006年 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Z小镇是一个景色宜人的地方,吸引来自各地的观 ...

  4. 学习redis-安装和基本一些命令

    redis安装 linux下环境安装redis,我这里下载的是3.0.0版本(目前最新版3.2.0). $ wget http://download.redis.io/releases/redis-3 ...

  5. mysql---union和左连接的两倒面试题

    第一道: 思路:无非是将hid与gid与t表中的tname关联起来.实质上是三表关联(m,t,t) 先将hid与tname关联起来,运用左连接 再将结果集与t表中的tname关联起来,使得gid与tn ...

  6. 排序算法FIVE:插入排序InsertSort

    /** *插入排序思路:O(n^2) * 最外层一个循环,从第二个数到最后一个,变量为i * 每个数存储在key变量中 * 变量j,是左边已经排好序的数组的上限 * 判断key与前面每一个数比较 1, ...

  7. 常用PHP运行环境一键安装包

    PHP的程序很火,像博客:Wordpress.论坛:Discuz!.SNS:UCenter Home.CMS:DedeCMS等等都是比较流行的PHP程序,大家都在使用PHP类的程序,但对于很多初学者来 ...

  8. 解决mac os x 10.9.1 AppStore ‘Use the Purchases page to try again’ 问题

    方法一: 关闭AppStore Terminal: open $TMPDIR/../C 删除 com.apple.appstore 下所有文件后进入AppStore重新下载 方法二: Terminal ...

  9. Pycharm使用技巧

    1.代码配色,即主题 pycharm自带的配色方案都很难看,网上的配色方案又很难看,所以根据其他ide的Monokai配色方案,自己定义了一个. pycharm Monokai主题下载:http:// ...

  10. .NET 轻量级 ORM 框架 - Dapper 介绍

    Dapper简单介绍: Dapper is a single file you can drop in to your project that will extend your IDbConnect ...