添加和处理动作(Adding and Handling Actions)
来源:互联网 发布:avast淘宝激活 编辑:程序博客网 时间:2024/05/16 04:43
这篇文章是接着上一篇文章翻译的,建议大家先看一看。 设置应用栏(Setting Up the App Bar)
本篇文章的原文地址:
http://developer.android.com/intl/zh-cn/training/appbar/actions.html#handle-actions
不扯别的,开搞
应用栏允许你为了处理用户的动作添加按钮。这个特性允许你把当前界面的最重要的动作的处理放在应用的顶部。
例如一个浏览照片的应用程序会在用户浏览图片列表的时候在应用的顶部展示“分享”和“创建相册”按钮;当用户查看一张照片的时候,应用程序会在顶部展示“剪裁图片”和“滤镜效果”的按钮。应用栏上的空间是有限的。如果actions按钮已经充满了应用栏,这时候用户再声明的acitons按钮会显示在voverflow菜单中。一个应用也可以明确的指出一个action按钮总是在overflow菜单中显示,而不是在应用栏上展示。
图表1:只有一个action按钮和一个voerflow菜单的应用栏
添加动作按钮 (Add Action Buttons)
在一个XML 菜单资源文件中定义的action按钮或者其他的item(不一定是按钮,也可能使一个搜索框)都可以在overflow菜单中使用。在你的res/menu/目录下新建一个xml文件来为应用栏添加actions.为每一个你想放入应用栏的item项添加一个元素,如下所示。
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <!-- "Mark Favorite", should appear as action button if possible --> <item android:id="@+id/action_favorite" android:icon="@drawable/ic_favorite_black_48dp" android:title="@string/action_favorite" app:showAsAction="ifRoom"/> <!-- Settings, should always be in the overflow --> <item android:id="@+id/action_settings" android:title="@string/action_settings" app:showAsAction="never"/>//永远在overflow中显示</menu>
app:showAsAction属性指出这个动作item 是否应该在应用栏上以按钮的形式出现。如果你设置了app:showAsAction=”ifRoom”(就像例子中的favorite action设置的那样),这个action 在应用栏上有足够的空间展示的时候,就会展示为一个按钮,如果没有足够的空间,就会被附加到overflow菜单中。如果你设置了app:showAsAction=”never”(就像例子中settings action设置的那样)这个action就会永远出现在overflow菜单中,而不会展示在应用栏上。
当 action被展示在应用栏上的时候,系统会使用这个action的icon来展示这个action。(所以你应该为每一个想在应用栏上展示的action设置一个android:icon=”@drawable/ic_favorite_black_48dp”属性)
你可以在Material Icons页面找到很多有用的图标。(https://design.google.com/icons/)
和Actions交互(Respond to Actions)
当用户选择应用栏上的一项时,系统会调用你的activity的onOptionsItemSelected()回调方法,并传递一个MenuItem来指出是哪一项被点击了。在你实现onOptionsItemSelected()的时候,调用 MenuItem.getItemId()来判断是那一项被按下了。返回的ID就是你在对应项的android:id attribute 定义的值。
例如 下列代码用来检查用户选择了那个action。如果这个方法不能识别用户的动作,就会调用父类的方法。
@Overridepublic boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_settings: // User chose the "Settings" item, show the app settings UI... return true; case R.id.action_favorite: // User chose the "Favorite" action, mark the current item // as a favorite... return true; default: // If we got here, the user's action was not recognized. // Invoke the superclass to handle it. return super.onOptionsItemSelected(item); }}
接下来我在实践中检验。在res/menu目录下新建一个mymenu.xml
mymenu.xml
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/action_favorite" android:icon="@drawable/ic_favorite_black_48dp" android:title="favorite" app:showAsAction="ifRoom" /> <item android:id="@+id/action_share" android:icon="@android:drawable/ic_menu_share" android:title="favorite" app:showAsAction="ifRoom" /><item android:id="@+id/action_settings" app:showAsAction="never" android:title="settings" /></menu></menu>
然后是MainActivity中的方法
@Override public boolean onCreateOptionsMenu(Menu menu) { //这句话的意思是把mymenu加载到menu中 getMenuInflater().inflate(R.menu.mymenu, menu); return super.onCreateOptionsMenu(menu); }//处理用户的点击事件 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_share: Toast.makeText(this, "我是share action", Toast.LENGTH_SHORT).show(); return true; case R.id.action_favorite: Toast.makeText(this, "我是favorite action", Toast.LENGTH_SHORT).show(); return true; case R.id.action_settings: Toast.makeText(this, "you click action_settings", Toast.LENGTH_SHORT).show(); return true; } return super.onOptionsItemSelected(item); }
效果图如下:
相关的代码没写全是因为在上一篇中也用到了,可以先看一看。对本篇翻译有疑问的欢迎询问。有错误欢迎指出,感激不尽。不能误人子弟啊。
快下班了,赶紧去吃饭,饿死了。
下一篇翻译: 添加一个向前导航的动作(Adding an Up Action)
- 添加和处理动作(Adding and Handling Actions)
- Adding Motion 添加动作
- 5.5.1.1 类型批注、动作和语句块(TYPE ANNOTATIONS, ACTIONS, AND STATEMENT BLOCKS)
- OpenERP Server Developers Documentation (Openerp服务器开发文档)之 Menus and Actions(菜单和动作)
- Silverlight 触发器与动作(Triggers and Actions)
- Silverlight 触发器与动作(Triggers and Actions)
- Silverlight 触发器与动作(Triggers and Actions)
- Actions 动作
- 异常和异常处理(windows平台)About Exceptions and Exception Handling
- MIPS 异常和中断处理(Exception and Interrupt handling)
- 添加一个向前导航的动作(Adding an Up Action)
- QT用信号和处理机制为事件添加处理动作(点击按钮显示helloworld)
- cocos2d-x:动作 Actions
- IDEA 中的 Intention Actions(意图动作) 上
- Struts2 官方教程:动作编程(Coding actions)
- PHP - Manual手册 - XXXIII. Error Handling and Logging Functions错误处理和日志函数 - 概述
- Actions, Controllers and Results(翻译)
- Magento——利用购物车促销规则(Shopping Cart Price Rules)实现自己的功能第一章: 添加新的动作(Actions)
- (MFC)CFont部分字体无法使用的问题
- 搭建Hadoop分布式
- struts-tags标签特殊用法
- Bestcoder #80 题解 (待补)
- 爬虫 取得站内最大页数
- 添加和处理动作(Adding and Handling Actions)
- (MFC)任务对话框CTaskDialog类
- android中的color文件
- MVC和MVP的梳理
- hbase0.98.9 Export工具使用
- __stdcall与__cdecl的区别
- ERROR: Cannot determine the location of the VS Common Tools folder.
- Java千百问_05面向对象(006)_is-a,has-a,like-a是什么
- 51Nod-1027-大数乘法