Android官方入门文档[6]添加Action按钮

简介: Android官方入门文档[6]添加Action按钮 Adding Action Buttons添加Action按钮 This lesson teaches you to1.

Android官方入门文档[6]添加Action按钮

 

Adding Action Buttons
添加Action按钮

 

This lesson teaches you to
1.Specify the Actions in XML
2.Add the Actions to the Action Bar
3.Respond to Action Buttons
4.Add Up Button for Low-level Activities

You should also read
•Providing Up Navigation


这节课教你
1.指定XML中的操作
2.给操作栏添加Action
3.响应Action按钮
4.为低级别的活动添加向上Up按钮

你也应该阅读
•提供向上导航

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.
操作栏允许您给应用程序的当前环境中最重要的行动项目添加的按钮。那些直接显示在操作栏中有一个图标和/或文字被称为操作按钮。Action在操作栏不适合或在Action溢出中不足够重要会被隐藏。

Figure 1. An action bar with an action button for Search and the action overflow, which reveals additional actions.
图1.与操作键搜索和action溢出,从而揭示更多动作一个动作吧。

 

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.
所有操作按钮,并在action溢出可用其他的数据项都以XML菜单资源中定义。将action动作添加到动作栏,创建在项目的res/menu/目录中一个新的XML文件。

Add an <item> element for each item you want to include in the action bar. For example:
添加<item>元素为你想要的操作栏包括每个项目。例如:

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.)
这宣告了搜索action应显示为一个动作按钮,当房间在操作栏可用,但设置action应始终出现在溢出。 (默认情况下,所有动作出现溢出,但它是很好的做法,明确声明你的设计意图的每个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.
图标属性要求的图像资源ID。随后@drawable/必须是你已经保存在你的项目的res/drawable/目录下的位图图像的名称。例如,
“@drawable/ ic_action_search”指ic_action_search.png。同样,title属性将使用的在你的项目的res/values/目录中的XML文件中定义,在构建一个简单的用户界面讨论字符串资源。

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.
注意:当你的应用程序创建的图标和其他位图图像,这一点很重要,你提供的每一个优化不同的屏幕密度的多个版本。这是更多的关于支持不同屏幕的课程进行讨论。

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系统的兼容性,showAsAction属性从android: namespace是不可用的。相反,这个属性是由支持库提供的,你必须定义自己的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
给操作栏添加Action


--------------------------------------------------------------------------------

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:
若要将菜单项到动作栏中,实现您的活动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
响应Action按钮


--------------------------------------------------------------------------------

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 <item> element's android:id attribute.
当用户按下其中一个操作按钮,或在Action溢出的另一个项,系统会调用您的活动的onOptionsItemSelected()回调方法。在实现此方法,调用给定菜单项getItemId(),以确定哪些项目被按下,返回的ID,你在相应的<item>元素的声明的价值相匹配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
添加向上Up按钮的低级别Activity活动


--------------------------------------------------------------------------------

 
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.
在您的应用程序的所有屏幕,不是你的应用程序的主要入口(活动不属于“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.
在Android4.1(API等级16)或更高的运行时,或者使用从支持库ActionBarActivity时,执行向上导航只是要求你申报的清单文件中的父活动,使向上按钮的操作栏。

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()使应用程序图标为向上Up按钮:

@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父活动,当用户按下向上键时,系统导航到父活动适当地-如果你不需要处理向上按钮的事件。

For more information about up navigation, see Providing Up Navigation.
有关向上导航的更多信息,请参阅提供向上导航。

本文翻译自:https://developer.android.com/training/basics/actionbar/adding-buttons.html

目录
相关文章
|
3月前
|
XML 数据库 Android开发
探索Android开发:从入门到精通的旅程
在这篇文章中,我们将一起踏上一段激动人心的旅程,通过深入浅出的方式,解锁Android开发的秘密。无论你是编程新手还是有经验的开发者,本文都将为你提供宝贵的知识和技能,帮助你构建出色的Android应用。我们将从基础概念开始,逐步深入到高级技巧和最佳实践,最终实现从初学者到专家的转变。让我们开始吧!
72 3
|
4月前
|
存储 前端开发 测试技术
Android kotlin MVVM 架构简单示例入门
Android kotlin MVVM 架构简单示例入门
63 1
|
4月前
|
开发框架 移动开发 Android开发
安卓与iOS开发中的跨平台解决方案:Flutter入门
【9月更文挑战第30天】在移动应用开发的广阔舞台上,安卓和iOS两大操作系统各自占据半壁江山。开发者们常常面临着选择:是专注于单一平台深耕细作,还是寻找一种能够横跨两大系统的开发方案?Flutter,作为一种新兴的跨平台UI工具包,正以其现代、响应式的特点赢得开发者的青睐。本文将带你一探究竟,从Flutter的基础概念到实战应用,深入浅出地介绍这一技术的魅力所在。
138 7
|
3月前
|
XML IDE Java
安卓应用开发入门:从零开始的旅程
【10月更文挑战第23天】本文将带领读者开启一段安卓应用开发的奇妙之旅。我们将从最基础的概念讲起,逐步深入到开发实践,最后通过一个简易的代码示例,展示如何将理论知识转化为实际的应用。无论你是编程新手,还是希望扩展技能的软件工程师,这篇文章都将为你提供有价值的指导和启发。
63 0
|
5月前
|
Android开发 开发者
安卓开发中的自定义视图:从入门到精通
【9月更文挑战第19天】在安卓开发的广阔天地中,自定义视图是一块充满魔力的土地。它不仅仅是代码的堆砌,更是艺术与科技的完美结合。通过掌握自定义视图,开发者能够打破常规,创造出独一无二的用户界面。本文将带你走进自定义视图的世界,从基础概念到实战应用,一步步展示如何用代码绘出心中的蓝图。无论你是初学者还是有经验的开发者,这篇文章都将为你打开一扇通往创意和效率的大门。让我们一起探索自定义视图的秘密,将你的应用打造成一件艺术品吧!
82 10
|
4月前
|
Web App开发 编解码 视频直播
视频直播技术干货(十二):从入门到放弃,快速学习Android端直播技术
本文详细介绍了Android端直播技术的全貌,涵盖了从实时音视频采集、编码、传输到解码与播放的各个环节。文章还探讨了直播中音视频同步、编解码器选择、传输协议以及直播延迟优化等关键问题。希望本文能为你提供有关Andriod端直播技术的深入理解和实践指导。
90 0
|
5月前
|
IDE Java 程序员
安卓应用开发入门:打造你的第一个“Hello World”
【9月更文挑战第11天】在编程的世界里,每一个初学者的旅程都从一个简单的“Hello World”开始。本文将带领安卓开发的新手们,通过简单直观的方式,一步步构建出自己的第一个安卓应用。我们将探索安卓工作室(Android Studio)的安装、项目的创建,以及如何运行和调试你的应用。无论你是编程新手还是想扩展技能的老手,这篇文章都将为你打开一扇通往安卓世界的大门。
239 7
|
5月前
|
IDE Java API
安卓应用开发入门:打造你的第一个"Hello World"
【9月更文挑战第11天】在探索安卓开发的海洋中,每个开发者的航行都从简单的"Hello World"开始。本文将作为你的航标,引导你驶向安卓应用开发的精彩世界。我们将一起启航,通过浅显易懂的语言和步骤,学习如何构建并运行你的第一个安卓应用。无论你是编程新手还是希望扩展技能的老手,这篇文章都将为你提供所需的知识和信心。准备好了吗?让我们揭开安卓开发的神秘面纱,一起创造些令人兴奋的东西吧!
|
6月前
|
开发者 iOS开发 C#
Uno Platform 入门超详细指南:从零开始教你打造兼容 Web、Windows、iOS 和 Android 的跨平台应用,轻松掌握 XAML 与 C# 开发技巧,快速上手示例代码助你迈出第一步
【8月更文挑战第31天】Uno Platform 是一个基于 Microsoft .NET 的开源框架,支持使用 C# 和 XAML 构建跨平台应用,适用于 Web(WebAssembly)、Windows、Linux、macOS、iOS 和 Android。它允许开发者共享几乎全部的业务逻辑和 UI 代码,同时保持原生性能。选择 Uno Platform 可以统一开发体验,减少代码重复,降低开发成本。安装时需先配置好 Visual Studio 或 Visual Studio for Mac,并通过 NuGet 或官网下载工具包。
626 0
|
6月前
|
XML IDE Java
安卓应用开发入门:打造你的第一个“Hello World”
【8月更文挑战第31天】 在安卓的浩瀚宇宙中,每一个新星都从一句简单的问候开始闪耀。本文将作为你的航标,引导你探索安卓开发的银河系。无论你是初心者还是资深开发者,构建第一个“Hello World”应用总是令人兴奋的里程碑。通过这篇文章,我们将一起搭建起通往安卓开发世界的桥梁。让我们摒弃复杂的术语,用最简单直白的语言,一步步地走过这段旅程。准备好了吗?让我们一起开启这段冒险吧!

热门文章

最新文章

  • 1
    如何修复 Android 和 Windows 不支持视频编解码器的问题?
  • 2
    【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 3
    当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
  • 4
    【04】flutter补打包流程的签名过程-APP安卓调试配置-结构化项目目录-完善注册相关页面-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程
  • 5
    APP-国内主流安卓商店-应用市场-鸿蒙商店上架之必备前提·全国公安安全信息评估报告如何申请-需要安全评估报告的资料是哪些-优雅草卓伊凡全程操作
  • 6
    【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 7
    Android经典面试题之Kotlin中Lambda表达式和匿名函数的区别
  • 8
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 9
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
  • 10
    Android学习自定义View(四)——继承控件(滑动时ListView的Item出现删除按钮)