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 barMenuInflater 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 itemsswitch (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) --><activityandroid:name="com.example.myfirstapp.MainActivity" ...>...</activity><!-- A child of the main activity --><activityandroid: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-dataandroid: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 官方说明文档,Android官方文档翻译-Accessibility

    标签元素 向用户提供解释每个可互动元素的意义和目的有用且形象的标签是非常重要的.这些标签允许屏幕阅读者(比如 TalkBack )正确向用户解释每个控制器的功能. 你可以使用一下两个方法提供元素的标签 ...

  2. Android官方文档翻译-Broadcasts

    原文链接:https://developer.android.com/guide/components/broadcasts.html 广播 Android应用可以向Android系统和其他Andro ...

  3. Android官方文档翻译 五 1.3Building a Simple User Interface

    Building a Simple User Interface 创建一个简单的用户界面 This lesson teaches you to 这节课将教给你: Create a Linear Lay ...

  4. Android官方文档翻译 十三 3.1Supporting Different Languages

    Supporting Different Languages 支持不同语言 This class teaches you to 这节课教给你 Create Locale Directories and ...

  5. android官方文档翻译,Android API Guide:Search 中文翻译

    8种机械键盘轴体对比 本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选? 搜索,是安卓的一个核心功能.用户可以搜索他们所有能获得的数据,无论这些内容在设备上或者在网络上. Search Over ...

  6. Android官方文档翻译 十二 3.Supporting Different Devices

    Supporting Different Devices 支持不同设备 Dependencies and prerequisites 依赖关系和先决条件 Android 1.6 or higher A ...

  7. Log4j官方文档翻译(九、输出到数据库)

    log4j提供了org.apache.log4j.JDBCAppender对象,可以把日志输出到特定的数据库. 常用的属性: bufferSize 设置buffer的大小,默认是1 driver 设置 ...

  8. Injective Protocol官方文档翻译(九) -清盘、清算(Liquidation)

    文章目录 一.清盘.清算(Liquidation) 1. 用订单清算头寸(liquidatePositionWithOrders) 一.清盘.清算(Liquidation) liquidation 英 ...

  9. Android官方导航栏ActionBar(二)—— Action View、Action Provider、Navigation Tabs的详细用法...

    在上一篇文章(Android之官方导航栏ActionBar)中,我们介绍了ActionBar各组成部分的基本应用.ActionBar除了提供Action Buttons外,还提供了多种导航方式如 Ac ...

最新文章

  1. python getattr函数_[转]Python中的getattr()函数详解
  2. WebRTC / Jitsi / 架构
  3. aliyun maven 添加jar_阿里云Maven配置,Maven仓库配置,Maven镜像配置
  4. 国家开放大学2021春1087数学分析专题研究题目
  5. stm32g4 下载算法_难道STM32G4芯片的主频才到80MHz?
  6. linux内存管理详解,Linux内存管理图文讲解.pdf
  7. 小米12系列渲染图曝光:双曲面屏+屏下摄像头
  8. 代码整洁之道(二)优雅注释之道
  9. python定义test方法_关于python:使用pytest测试类方法
  10. SpringBoot 快速整合 QuartZ
  11. 贝壳完成在香港双重主要上市:市值超1200亿港元
  12. html页面右下角添加js广告,JS右下角悬浮广告代码的简单示例
  13. Unity联机人物加入游戏、同屏移动、与攻击
  14. HTML菜鸟教程学习笔记
  15. chrome下载文件竟然都有缓存!
  16. 《opencv学习笔记》-- 感兴趣区域(ROI)、图像混合
  17. [并发并行]_[线程池]_[Programming With POSIX Threads的线程池实现分析1]
  18. Quartz 实现画图片、写文字、画线、椭圆、矩形、棱形等。
  19. matlab subs什么意思,什么是matlab subs函数?
  20. http://www.dewen.net.cn/q/15328/问个正则表达式 贪婪 和 不匹配某个字符串问题

热门文章

  1. [旧博客]QQ旋风加速漏洞
  2. Mac下如何用SSH连接远程Linux服务器
  3. 罗斯蒙特PH电极1056-01-22-32-AN智能型四线制变送器规格及特点
  4. android cmwap 切换 cmnet,Android CMWAP和CMNET 切换APN
  5. 陕西国防 c语言第三章实训三答案,C语言程序设计实验指导
  6. 笔试题——用java实现股票交易日的判断
  7. python返回上一个交易日
  8. 创新ICT促进行业迈向云时代
  9. tomcat定时自动重启设置方法
  10. 同步屏障Barrier