Adding Action Buttons

增加动作按钮

This lesson teaches you to

这节课教给你

  1. Specify the Actions in XML

    在XML中指定动作

  2. Add the Actions to the Action Bar

    把动作添加到状态栏

  3. Respond to Action Buttons

    让动作按钮有响应

  4. 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的更多相关文章

  1. 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 ...

  2. Android官方文档翻译 七 2.Adding the Action Bar

    Adding the Action Bar 增加一个Action Bar(工具栏) The action bar is one of the most important design element ...

  3. Android官方文档翻译 十 2.3Styling the Action Bar

    Styling the Action Bar 设计菜单栏的样式 This lesson teaches you to 这节课教给你 Use an Android Theme 使用一个Android主题 ...

  4. 【Android Developers Training】 7. 添加Action Buttons

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  5. android官方文档翻译(不断更新中。。。)

    最近在自学android,抽空把官方文档的guide跟training差不多看了一遍,又对比了一些书籍,感觉还是官方文档讲得比较好,所以自己计划把官方文档翻译一下,方便自己的知识巩固以及复习查找,由于 ...

  6. Android官方文档翻译 十一 2.4Overlaying the Action Bar

    Overlaying the Action Bar 叠加菜单栏 This lesson teaches you to 这节课教给你: Enable Overlay Mode 启用叠加模式 For An ...

  7. Android官方文档翻译 十七 4.1Starting an Activity

    Starting an Activity 开启一个Activity This lesson teaches you to 这节课教给你 Understand the Lifecycle Callbac ...

  8. Android官方文档翻译 十六 4.Managing the Activity Lifecycle

    Managing the Activity Lifecycle 管理activity的生命周期 Dependencies and prerequisites 依赖关系和先决条件 How to crea ...

  9. Android官方文档翻译 十五 3.3Supporting Different Platform Versions

    Supporting Different Platform Versions 支持不同的平台版本 This lesson teaches you to 这节课教给你 Specify Minimum a ...

随机推荐

  1. MySQL管理之道,性能调优,高可用与监控(第二版)pdf下载

    MySQL管理之道,性能调优,高可用与监控(第二版) 书中内容以实战为导向,所有内容均来自于笔者多年实践经验的总结和新知识的拓展,同时也针对运维人员.DBA等相关工作者会遇到的有代表性的疑难问题给出了 ...

  2. ASP.NET VS 调试提示:指定的端口正在使用中,建议切换到xxx之外并大于1024的端口

    问题描述 使用 Visual Studio 开发 ASP.NET 网站的过程中,突然提示端口被占用: 解决方式 在启动项目上右键→属性,切换到 Web .直接修改服务器栏目里面的端口号,解决!

  3. HTTP强缓存和协商缓存

    一.浏览器缓存 Web 缓存能够减少延迟与网络阻塞,进而减少显示某个资源所用的时间.借助 HTTP 缓存,Web 站点变得更具有响应性. (一).缓存优点: 减少不必要的数据传输,节省带宽 减少服务器 ...

  4. 一台电脑如何安装多个版本的JDK及其说明

    一.安装多个版本的JDK及其说明 1.准备多个版本的JDK 需要请点击下载 2.安装过程,除了根据需要更改安装路径,其他"傻瓜式"安装("下一步"),各版本都是 ...

  5. CF1501A Alexey and Train 题解

    Content 一列火车从 \(0\) 时刻开始从 \(1\) 号站出发,要经过 \(n\) 个站,第 \(i\) 个站的期望到达时间和离开时间分别为 \(a_i\) 和 \(b_i\),并且还有一个 ...

  6. worksocket使用Nginx进行反向代理

    配置文件 map $http_upgrade $connection_upgrade { default upgrade; '' close; } upstream weoskceturl{ serv ...

  7. UDP&串口调试助手用法(3)

    发送参数配置 下面以 UDP 通道为例介绍 发送数据配置 概览 选择数据源 文件: 选择发送的文件 源码: 自己手动键入发送数据,默认输入的为16进制数据, 定时器发送周期 单位为毫秒. 发送帧计数 ...

  8. nim_duilib(15)之duilib属性列表.xml

    Note 为了更加方便查看duilib的属性(github有时候打不开),特此记录. 阅读本文,可以知道控件有哪些属性,可以写在xml文件中.个别需要结合源码一起看 from here 原文 < ...

  9. Robin Hood

    Robin Hood 题目链接 题意 给你n个人和他们的钱数,然后给你k天,每天可以从最高钱数的人那边取一块钱给最少钱数的人,问最后钱数最多的人和钱数最少的人相差多少: 思路 二分最钱数,能下降到的位 ...

  10. 【C++】关于new分配空间

    1如果不使用new,则在函数结束时内存被回收,指针变成野指针 #include <iostream> using namespace std; struct Node { int val; ...