Android官方文档翻译 九 2.2Adding Action Buttons
Adding Action Buttons
增加动作按钮
This lesson teaches you to
这节课教给你
Specify the Actions in XML
在XML中指定动作
Add the Actions to the Action Bar
把动作添加到状态栏
Respond to Action Buttons
让动作按钮有响应
Add Up Button for Low-level Activities
对低版本的activities增加顶部按钮
You should also read
你还应该读
- Providing Up Navigation
提供顶部导航
The action bar allows you to add buttons for 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.
状态栏允许你把大部分重要的动作项目的按钮添加在其上以和应用程序的当前上下文环境相关联。这些可以直接在状态栏上显示一个用来辨别动作按钮的图标或者文本。在状态栏上装不下的动作按钮或者不是足够重要的动作按钮可以隐藏在动作溢出夹(更多操作)中。
Figure 1. An action bar with an action button for Search and the action overflow, which reveals additional actions.
图1 拥有一个查找动作按钮和一个动作溢出夹(更多操作)的一个状态栏,它提供了额外的动作。
Specify the Actions in XML
在XML中指定动作
All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project’s res/menu/ directory.
所有的动作按钮和其它一些放在动作溢出夹(更多操作)中的可用项目都需要在一个XML菜单资源中定义。为了在状态栏上添加动作,在你的项目的 res/menu/目录下创建一个新的XML文件。
Add an element for each item you want to include in the action bar. For example:
为你想要在状态栏中添加的每个项目增加一个元素。例如:
res/menu/main_activity_actions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" />
</menu>
Download action bar icons
下载状态栏图标
To best match the Android iconography guidelines, you should use icons provided in the Action Bar Icon Pack.
为了最好的适配Android图解向导,你应该使用状态栏图标包里提供的图标。(官方网站可以下载)
This declares that the Search action should appear as an action button when room is available in the action bar, but the Settings action should always appear in the overflow. (By default, all actions appear in the overflow, but it’s good practice to explicitly declare your design intentions for each action.)
这里声明:当状态栏有空间可用时显示搜索按钮,但是设置按钮应该总是在隐藏夹中出现。(默认情况下,所有的动作都在隐藏夹中出现,但是这对于你显式的声明你对每个动作的设计意图是很好的一个练习。)
The icon attribute requires a resource ID for an image. The name that follows @drawable/ must be the name of a bitmap image you’ve saved in your project’s res/drawable/ directory. For example, “@drawable/ic_action_search” refers to ic_action_search.png. Likewise, the title attribute uses a string resource that’s defined by an XML file in your project’s res/values/ directory, as discussed in Building a Simple User Interface.
icon 属性需要一个图片资源ID。它的名字跟在 @drawable/后面,这个名字必须是你已经在你的项目的res/drawable/ 目录下保存的一个位图图像的名字。例如,“@drawable/ic_action_search”引用的是ic_action_search.png这张图片。同样地,title属性使用一个string资源,这个资源也是你在你的项目的res/values/目录下的一个XML文件中定义好的,就如我们之前在Building a Simple User Interface中讨论的那样。
Note: When creating icons and other bitmap images for your app, it’s important that you provide multiple versions that are each optimized for a different screen density. This is discussed more in the lesson about Supporting Different Screens.
注意:当你在你的应用程序中创建图标和其他的位图图像时,对于不同的屏幕密度的设备提供多种版本用来优化是很重要的。这在课程Supporting Different Screens课程中将会学到更多。
If your app is using the Support Library for compatibility on versions as low as Android 2.1, the showAsAction attribute is not available from the android: namespace. Instead this attribute is provided by the Support Library and you must define your own XML namespace and use that namespace as the attribute prefix. (A custom XML namespace should be based on your app name, but it can be any name you want and is only accessible within the scope of the file in which you declare it.) For example:
如果你的应用程序为了兼容Android 2.1以下版本而使用了Support Library,在android:中的命名空间中showAsAction属性是不可用的。Support Library提供了这个属性用来代替,你必须定义你自己的XML命名空间,然后使用这个命名空间作为这个属性的前缀。(一个自定义的XML命名空间应该以你的应用程序名字为基础,但是它可以使用任何你想用的名字来定义,并且它仅仅在你生命的这个文件范围内才能访问。)例如:
res/menu/main_activity_actions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
yourapp:showAsAction="ifRoom" />
...
</menu>
Add the Actions to the Action Bar
给状态栏添加动作
To place the menu items into the action bar, implement the onCreateOptionsMenu() callback method in your activity to inflate the menu resource into the given Menu object. For example:
为了把菜单项目放置到状态栏上,需要在你的activity中实现onCreateOptionsMenu()这个回调方法,以把菜单资源加载到给定的菜单对象上。例如:
@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);
}
Respond to Action Buttons
对动作按钮做出响应
When the user presses one of the action buttons or another item in the action overflow, the system calls your activity’s onOptionsItemSelected() callback method. In your implementation of this method, call getItemId() on the given MenuItem to determine which item was pressed—the returned ID matches the value you declared in the corresponding element’s android:id attribute.
当用户点击其中一个动作按钮或者动作溢出栏(更多操作)中的一个项目时,系统会调用你的activity中的onCreateItemSelected()回调方法。在这个方法中你要实现的是,在给定的菜单条目中调用getItemId()来确定哪个条目被点击了—-返回的ID和你在相应的元素中的android:id 属性定义的值相匹配。
@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_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Add Up Button for Low-level Activities
对低版本的activities增加顶部按钮
Figure 4. The Up button in Gmail.
图4 Gmail中的顶部按钮
All screens in your app that are not the main entrance to your app (activities that are not the “home” screen) should offer the user a way to navigate to the logical parent screen in the app’s hierarchy by pressing the Up button in the action bar.
在你的应用程序中,对于所有不是主入口的屏幕(activities不是“home”屏幕)都应该提供给用户一个方式导航跳转到它的逻辑父屏幕,这些要通过在工具栏上点击按钮来实现应用程序中的这种层次结构。
When running on Android 4.1 (API level 16) or higher, or when using ActionBarActivity from the Support Library, performing Up navigation simply requires that you declare the parent activity in the manifest file and enable the Up button for the action bar.
当在Android 4.1(API 16)及其以上运行,或者当使用Support Library提供的ActionBarActivity时,执行顶部的导航菜单仅仅需要你在manifest文件中定义它的父activity,并且让其成为工具栏的顶部按钮。
For example, here’s how you can declare an activity’s parent in the manifest:
例如,以下是你如何在manifest中定义一个activity的父亲:
<application ... >
...
<!-- The main/home activity (it has no parent activity) -->
<activity
android:name="com.example.myfirstapp.MainActivity" ...>
...
</activity>
<!-- A child of the main activity -->
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
Then enable the app icon as the Up button by calling setDisplayHomeAsUpEnabled():
然后通过调用setDisplayHomeAsUpEnabled()让其可用app的图标作为顶部按钮:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_displaymessage);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// If your minSdkVersion is 11 or higher, instead use:
// getActionBar().setDisplayHomeAsUpEnabled(true);
}
Because the system now knows MainActivity is the parent activity for DisplayMessageActivity, when the user presses the Up button, the system navigates to the parent activity as appropriate—you do not need to handle the Up button’s event.
因为现在系统知道了MainActivity是DisplayMessageActivity的父activity,所以当用户点击顶部按钮时,系统会视情况导航跳转到它的父activity—-你根本不需要处理顶部按钮的事件。
For more information about up navigation, see Providing Up Navigation.
想知道关于顶部导航的更多信息,请看Providing Up Navigation。
NEXT: STYLING THE ACTION BAR
下一节:给菜单栏设计样式
这些是我自己翻译的,如果您发现其中有重要错误,敬请指出,万分感谢!
Android官方文档翻译 九 2.2Adding Action Buttons的更多相关文章
- Android官方文档翻译 八 2.1Setting Up the Action Bar
Setting Up the Action Bar 建立Action Bar This lesson teaches you to 这节课教给你 Support Android 3.0 and Abo ...
- Android官方文档翻译 七 2.Adding the Action Bar
Adding the Action Bar 增加一个Action Bar(工具栏) The action bar is one of the most important design element ...
- Android官方文档翻译 十 2.3Styling the Action Bar
Styling the Action Bar 设计菜单栏的样式 This lesson teaches you to 这节课教给你 Use an Android Theme 使用一个Android主题 ...
- 【Android Developers Training】 7. 添加Action Buttons
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- android官方文档翻译(不断更新中。。。)
最近在自学android,抽空把官方文档的guide跟training差不多看了一遍,又对比了一些书籍,感觉还是官方文档讲得比较好,所以自己计划把官方文档翻译一下,方便自己的知识巩固以及复习查找,由于 ...
- Android官方文档翻译 十一 2.4Overlaying the Action Bar
Overlaying the Action Bar 叠加菜单栏 This lesson teaches you to 这节课教给你: Enable Overlay Mode 启用叠加模式 For An ...
- Android官方文档翻译 十七 4.1Starting an Activity
Starting an Activity 开启一个Activity This lesson teaches you to 这节课教给你 Understand the Lifecycle Callbac ...
- Android官方文档翻译 十六 4.Managing the Activity Lifecycle
Managing the Activity Lifecycle 管理activity的生命周期 Dependencies and prerequisites 依赖关系和先决条件 How to crea ...
- Android官方文档翻译 十五 3.3Supporting Different Platform Versions
Supporting Different Platform Versions 支持不同的平台版本 This lesson teaches you to 这节课教给你 Specify Minimum a ...
随机推荐
- windows下python3.7安装gmpy2、Crypto 库及rsa
基于python3.7在windows下安装gmpy2 先检查一下是否安装了wheel文件包,在cmd中输入wheel,查看一下,如果没有安装,则输入安装:pip install wheel 如果遇到 ...
- LuoguP7222 [RC-04] 信息学竞赛 题解
Content 给定一个角 \(\alpha\),求 \(\beta=90^\circ-\alpha\). 数据范围:\(\alpha\in[-2^{31},2^{31}-1]\). Solution ...
- props 使用场景 及 布局提升
一对一一边写html 一边写css一小块为单位html csscss html整块单位html csscss html react/first-react/src/views/Wk/index.jsx ...
- 使用.NET 6开发TodoList应用文章索引
系列导航 使用.NET 6开发TodoList应用(1)--系列背景 使用.NET 6开发TodoList应用(2)--项目结构搭建 使用.NET 6开发TodoList应用(3)--引入第三方日志 ...
- SpringBoot 整合es(elasticsearch)使用elasticsearch-rest-high-level-client实现增删改
引入依赖 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok< ...
- video标签实现多个视频循环播放
<head> <!-- If you'd like to support IE8 (for Video.js versions prior to v7) --> </he ...
- 【LeetCode】244. Shortest Word Distance II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存出现位置 日期 题目地址:https://le ...
- 【LeetCode】1041. Robot Bounded In Circle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找规律 日期 题目地址:https://leetco ...
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 重重封锁,让你一条数据都拿不到《死磕MySQL系列 十三》
在开发中有遇到很简单的SQL却执行的非常慢,甚至只查询一行数据. 咔咔遇到的只有两种情况,一种是MySQL服务器CPU占用率很高,所有的SQL都执行的很慢直到超时,程序也直接502,另一种情况是行锁造 ...