ActionBar官方教程(7)自定义操作项的view,如何得到它及处理它的事件
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,如何得到它及处理它的事件的更多相关文章
- ActionBar官方教程(11)自定义ActionBar的样式(含重要的样式属性表及练习示例)
Styling the Action Bar If you want to implement a visual design that represents your app's brand, th ...
- ActionBar官方教程(8)ShareActionProvider与自定义操作项提供器
Adding an Action Provider Similar to an action view, an action provider replaces an action button wi ...
- ActionBar官方教程(4)给ActionBar添加操作项及它们的事件处理
Adding Action Items The action bar provides users access to the most important action items relating ...
- ContentProvider官方教程(8)自定义MIME
MIME Type Reference Content providers can return standard MIME media types, or custom MIME type stri ...
- 自定义View(7)官方教程:自定义View(含onMeasure),自定义一个Layout(混合组件),重写一个现有组件
Custom Components In this document The Basic Approach Fully Customized Components Compound Controls ...
- ActionBar官方教程(10)ActionBar的下拉列表模式
Adding Drop-down Navigation As another mode of navigation (or filtering) for your activity, the acti ...
- ActionBar官方教程(9)ActionBar的顶部tab模式(注意,已经被弃用)
This interface is deprecated.Action bar navigation modes are deprecated and not supported by inline ...
- 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 ...
- ActionBar官方教程(5)ActionBar的分裂模式(底部tab样式),隐藏标题,隐藏图标
Using split action bar Split action bar provides a separate bar at the bottom of the screen to displ ...
随机推荐
- Android--LowMemoryKiller知识点补充
Android在内存管理上与linux有些小的区别.其中一个就是引入了Low memory killer . 1.引入原因: Android是一个多任务系统,也就是说可以同时运行多个程序,这个大家应该 ...
- Android笔记之adb命令应用实例1(手机端与PC端socket通讯上)
Android端的代码: 布局文件:activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/ ...
- java培训(1-4节课)
课程安排:JavaEE方向(控制台程序,GUI程序,Web程序,手机程序)(dos命令是控制台程序:QQ是GUI程序,放在计算机上:QQ空间是Web程序,放在腾讯公司) 讲课的13本教材:C语言,Ja ...
- crontab环境变量问题
今天设置linux定时任务时,python内调用的shell指令总执行失败,单独调用python脚本则无问题,考虑到是环境变量未生效引起. 故在执行crontab -e编辑配置文件时,将shell内执 ...
- Lua脚本之语法基础快速入门
要 1.基本数据类型 2.Lua中的常用语句结构以及函数 3.Lua中的常用语句结构介绍 4.Lua中的库函数 目录[-] 一.基本数据类型 二.Lua中的常用语句结构以及函数 1.Lua中的常用语句 ...
- Linux C 程序 函数,数组,指针,gdb调试器(SEVEN)
函数,数组,指针,gdb调试器 1.函数定义 如果明确指定返回类型,默认为int 参数传递:实参对形参的参数传递是单向的,实参只是把自己的值赋给形参. 形参的 ...
- Sublime Text 3的快捷键
Sublime Text 3是一个非常了不起的软件,它不仅具有令人难以置信的内置功能(多行编辑和VIM模式),而且还支持插件.代码片段和其它许多东西. 今天,我们来总结一下Sublime Text 3 ...
- WCF SOA服务应用
WCF是微软官方推出的一个基于服务的整合框架,它整合了以前的Web Service.MSMQ.Remoting等通信技术,通过灵活的配置,让服务编程更加容易.可扩展.这篇文章主要目的就是带领大家从开发 ...
- php eval函数用法总结
可以在php.ini禁止此函数,结果失败了 eval定义和用法 eval() 函数把字符串按照 PHP 代码来计算. 该字符串必须是合法的 PHP 代码,且必须以分号结尾. 如果没有在代码字符串 ...
- Authentication for the REST APIs
HTTP基本认证原理 在HTTP协议进行通信的过程中,HTTP协议定义了基本认证过程以允许HTTP服务器对WEB浏览器进行用户身份认证的方法,当一个客户端向HTTP服务器进行数据请求时,如果客户端未被 ...