Adding an Action View


  An action view is a widget that appears in the action bar as a substitute for an action button. An action view provides fast access to rich actions without changing activities or fragments, and without replacing the action bar. For example, if you have an action for Search, you can add an action view to embeds a SearchView widget in the action bar, as shown in figure 5.

Figure 5. An action bar with a collapsible SearchView.

  To declare an action view, use either the actionLayout or actionViewClass attribute to specify either a layout resource or widget class to use, respectively. For example, here's how to add the SearchView widget:

 <?xml version="1.0" encoding="utf-8"?>
 <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:title="@string/action_search"
           android:icon="@drawable/ic_action_search"
           yourapp:showAsAction="ifRoom|collapseActionView"
           yourapp:actionViewClass="android.support.v7.widget.SearchView" />
 </menu>

  Notice that the showAsAction attribute also includes the "collapseActionView" value. This is optional and declares that the action view should be collapsed into a button. (This behavior is explained further in the following section about Handling collapsible action views.)

  android:showAsAction="collapseActionView" 是可折叠自定义的view

  If you need to configure the action view (such as to add event listeners), you can do so during the onCreateOptionsMenu() callback. You can acquire the action view object by calling the static method MenuItemCompat.getActionView() and passing it the corresponding MenuItem. For example, the search widget from the above sample is acquired like this:

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
     getMenuInflater().inflate(R.menu.main_activity_actions, menu);
     MenuItem searchItem = menu.findItem(R.id.action_search);
     SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
     // Configure the search info and add any event listeners
     ...
     return super.onCreateOptionsMenu(menu);
 }
  在api 11 以前 可以在onCreateOptionsMenu()函数中,通过MenuItemCompat.getActionView(xxItem) 得到它自定义的view,然后给这个view注册事件函数等.

On API level 11 or higher

  Get the action view by calling getActionView() on the corresponding MenuItem:

menu.findItem(R.id.action_search).getActionView();
  在api 11 以后 可以在onCreateOptionsMenu()函数中,直接通过item.getActionView() 得到它自定义的view,然后给这个view注册事件函数等.

  For more information about using the search widget, see Creating a Search Interface.

Handling collapsible action views

  To preserve the action bar space, you can collapse your action view into an action button. When collapsed, the system might place the action into the action overflow, but the action view still appears in the action bar when the user selects it. You can make your action view collapsible by adding "collapseActionView" to the showAsAction attribute, as shown in the XML above.

  Because the system expands the action view when the user selects the action, you do not need to respond to the item in the onOptionsItemSelected() callback. The system still calls onOptionsItemSelected(), but if you return true (indicating you've handled the event instead), then the action view will not expand.

  The system also collapses your action view when the user presses the Up button or Back button.

  If you need to update your activity based on the visibility of your action view, you can receive callbacks when the action is expanded and collapsed by defining an OnActionExpandListener and passing it to setOnActionExpandListener(). For example:

  collapseActionView 属性值表示把自定义的action view折叠到溢出菜单中.每个操作项可以注册折叠事件接口 OnActionExpandListener 得到

  展开事件回调: onMenuItemActionExpand

  折叠事件回调: onMenuItemActionCollapse
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
     getMenuInflater().inflate(R.menu.options, menu);
     MenuItem menuItem = menu.findItem(R.id.actionItem);
     ...

     // When using the support library, the setOnActionExpandListener() method is
     // static and accepts the MenuItem object as an argument
     MenuItemCompat.setOnActionExpandListener(menuItem, new OnActionExpandListener() {
         @Override
         public boolean onMenuItemActionCollapse(MenuItem item) {
             // Do something when collapsed
             return true;  // Return true to collapse action view
         }

         @Override
         public boolean onMenuItemActionExpand(MenuItem item) {
             // Do something when expanded
             return true;  // Return true to expand action view
         }
     });
 }

ActionBar官方教程(7)自定义操作项的view,如何得到它及处理它的事件的更多相关文章

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

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

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

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

  3. ActionBar官方教程(4)给ActionBar添加操作项及它们的事件处理

    Adding Action Items The action bar provides users access to the most important action items relating ...

  4. ContentProvider官方教程(8)自定义MIME

    MIME Type Reference Content providers can return standard MIME media types, or custom MIME type stri ...

  5. 自定义View(7)官方教程:自定义View(含onMeasure),自定义一个Layout(混合组件),重写一个现有组件

    Custom Components In this document The Basic Approach Fully Customized Components Compound Controls ...

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

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

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

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

  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. iOS 各种控件默认高度

    1.状态栏 状态栏一般高度为20像素,在打手机或者显示消息时会放大到40像素高,注意,两倍高度的状态栏在好像只能在纵向的模式下使用.如下图   用户可以隐藏状态栏,也可以将状态栏设置为灰色,黑色或者半 ...

  2. tomcat安装不成功-提示找不到JAVA虚拟机

    今天重装tomcate,但是总是提示找不到java虚拟机,但是我明明装了jre和jdk,太烦人了 后来搜了各种方法,终于找到了解决方法,现在和大家分享下 到提示找java虚拟机那一步的时候,选择到jr ...

  3. 九度OJ 1499 项目安排 -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1499 题目描述: 小明每天都在开源社区上做项目,假设每天他都有很多项目可以选,其中每个项目都有一个开始时间和截止时 ...

  4. win7上帝模式

    在win7 系统桌面或任意磁盘下新建文件夹,将文件夹改名为 GodModel.{ED7BA470-8E54-465E-825C-99712043E01C}

  5. SSH+Ajax实现用户名重复检查(一)

    1.struts.xml设置 <package name="default" namespace="/" extends="json-defau ...

  6. 恶心的学校机房SQL安装

    学校机房每台PC(DELL OPTIPLEX 380)上有两个系统,分别对应XP中英文版.管理员将500G硬盘分为两部分(两个主分区,两个逻辑分区),每个系统占用一个主分区和一个逻辑分区,主分区都有冰 ...

  7. csdn博客刷点击率代码

    此文为转载,亲测有效. import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; impo ...

  8. CentOS 6.4 安装SecurectCRT并破解

    经过验证此方法使用于Centos 系列. 相关说明: 上篇发了个Linux(Ubuntu) 下 SecureCRT 7 30天循环破解在启动的时候会多输入一次确认窗口, 后来maz-1网友留言说可以用 ...

  9. How to open .ccproj in VS2010?

    Q: How to open .ccproj projects types in VS2010, ccproj file type is a Cloud project i suppose. Plea ...

  10. typedef和#define的区别

    转自:http://www.cnblogs.com/kerwinshaw/archive/2009/02/02/1382428.html 一.typedef的用法在C/C++语言中,typedef常用 ...