Overlaying the Action Bar

叠加菜单栏

This lesson teaches you to

这节课教给你:

  1. Enable Overlay Mode

    启用叠加模式

    • For Android 3.0 and higher only

      对于仅支持Android 3.0及其以上版本

    • For Android 2.1 and higher

      对于支持Android 2.1及其以上版本

  2. Specify Layout Top-margin

    指定布局的顶边距

You should also read

你还需要阅读

  • Styles and Themes

样式和主题


By default, the action bar appears at the top of your activity window, slightly reducing the amount of space available for the rest of your activity’s layout. If, during the course of user interaction, you want to hide and show the action bar, you can do so by calling hide() and show() on the ActionBar. However, this causes your activity to recompute and redraw the layout based on its new size.

默认情况下,菜单栏展现在你的activity界面的顶部,这会轻微的减少你activity的布局的其他剩余空间的总数。通过用户界面的这节课程我们知道,如果你想要隐藏和展现菜单栏,你可以通过对菜单栏调用hide()和show()来实现。然而,这会造成你的activity要根据它的新的大小重新计算和绘制布局。

To avoid resizing your layout when the action bar hides and shows, you can enable overlay mode for the action bar. When in overlay mode, your activity layout uses all the space available as if the action bar is not there and the system draws the action bar in front of your layout. This obscures some of the layout at the top, but now when the action bar hides or appears, the system does not need to resize your layout and the transition is seamless.

为了避免在菜单栏隐藏和展示时重新调整布局,你可以启用菜单栏的叠加模式。在叠加模式中,就好像是菜单栏不在,你的activity布局会使用所有的可用空间,并且系统会在你的布局前面绘画出菜单栏。这会让顶部的一些布局变得模糊,但是现在当菜单栏隐藏或展现的时候,系统就不需要重新调整你的布局,可以实现无缝隙转换。

Tip: If you want your layout to be partially visible behind the action bar, create a custom style for the action bar with a partially transparent background, such as the one shown in figure 1. For information about how to define the action bar background, read Styling the Action Bar.

技巧:如果你想要你的布局部分在菜单栏后面显现,请用一个特别地透明背景给菜单栏创建一个自定义样式,比如图1中展现的那样。关于如何定义菜单栏的背景,请阅读Styling the Action Bar。

Figure 1. Gallery’s action bar in overlay mode.

图1 叠加模式中Gallery的菜单栏

Enable Overlay Mode

启用叠加模式

To enable overlay mode for the action bar, you need to create a custom theme that extends an existing action bar theme and set the android:windowActionBarOverlay property to true.

为了启用菜单栏的叠加模式,你需要创建一个自定义主题,让它继承一个已经存在了的菜单栏主题,然后设置android:windowActionBarOverlay属性为真。

For Android 3.0 and higher only

对于仅仅支持Android 3.0及其以上版本

If your minSdkVersion is set to 11 or higher, your custom theme should use Theme.Holo theme (or one of its descendants) as your parent theme. For example:

如果你的minSDKVersion设置为11或者更高,你的自定义主题应该使用Theme.Holo主题(或者它的后代中的一个)作为你的父主题。例如:

<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="@android:style/Theme.Holo">
<item name="android:windowActionBarOverlay">true</item>
</style>
</resources>

For Android 2.1 and higher

对于支持Android 2.1或者更高版本

If your app is using the Support Library for compatibility on devices running versions lower than Android 3.0, your custom theme should use Theme.AppCompat theme (or one of its descendants) as your parent theme. For example:

如果你的应用程序使用Support Library以兼容运行低于Android 3.0版本的机器,你的自定义主题应该使用 Theme.Appcompat主题(或者它的后代之一)作为你的父主题。例如:

<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="@android:style/Theme.AppCompat">
<item name="android:windowActionBarOverlay">true</item> <!-- Support library compatibility -->
<item name="windowActionBarOverlay">true</item>
</style>
</resources>

Also notice that this theme includes two definitions for the windowActionBarOverlay style: one with the android: prefix and one without. The one with the android: prefix is for versions of Android that include the style in the platform and the one without the prefix is for older versions that read the style from the Support Library.

你还可以注意到这个主题包含对于windowActionBarOverlay样式的两种定义:一种以android:作为其前缀,另一种则没有。以android:作为前缀的那种是对于在平台中已经包含此样式的Android版本,另一种没有前缀的是对于旧版本,需要在Support Library中读取这个主题。

Specify Layout Top-margin

指定布局的顶边距

When the action bar is in overlay mode, it might obscure some of your layout that should remain visible. To ensure that such items remain below the action bar at all times, add either margin or padding to the top of the view(s) using the height specified by actionBarSize. For example:

在叠加模式中,菜单栏可能会遮盖你的一些本来应该保持可见的布局。为了保证这些项目在所有的时间都保持低于菜单栏,需要使用actionBarSize已经定义好的高度给顶部的视图添加边界或者内边距。例如:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?android:attr/actionBarSize">
...
</RelativeLayout>

If you’re using the Support Library for the action bar, you need to remove the android: prefix. For example:

如果你使用Support Library中的菜单栏,你需要移除android:这个前缀。例如:

<!-- Support library compatibility -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
...
</RelativeLayout>

In this case, the ?attr/actionBarSize value without the prefix works on all versions, including Android 3.0 and higher.

在这种情况下,?attr/actionBarSize值在所有版本中都没有前缀,包括Android 3.0 及其以上版本。

NEXT CLASS: SUPPORTING DIFFERENT DEVICES

下一课:支持不同设备

这是我自己翻译的,如果您发现其中有重要错误,敬请指出,万分感谢。

Android官方文档翻译 十一 2.4Overlaying the Action Bar的更多相关文章

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

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

  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.1Setting Up the Action Bar

    Setting Up the Action Bar 建立Action Bar This lesson teaches you to 这节课教给你 Support Android 3.0 and Abo ...

  4. Android UI开发第二十四篇——Action Bar

    Action bar是一个标识应用程序和用户位置的窗口功能,并且给用户提供操作和导航模式.在大多数的情况下,当你需要突出展现用户行为或全局导航的activity中使用action bar,因为acti ...

  5. 【转】Android UI开发第二十四篇——Action Bar

    Action bar是一个标识应用程序和用户位置的窗口功能,并且给用户提供操作和导航模式.在大多数的情况下,当你需要突出展现用户行为或全局导航的activity中使用action bar,因为acti ...

  6. Android官方文档翻译 九 2.2Adding Action Buttons

    Adding Action Buttons 增加动作按钮 This lesson teaches you to 这节课教给你 Specify the Actions in XML 在XML中指定动作 ...

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

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

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

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

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

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

随机推荐

  1. 利用模块加载回调函数修改PE导入表实现注入

    最近整理PE文件相关代码的时候,想到如果能在PE刚刚读进内存的时候再去修改内存PE镜像,那不是比直接对PE文件进行操作隐秘多了么? PE文件在运行时会根据导入表来进行dll库的"动态链接&q ...

  2. M语言中的引用(Power Query 之 M 语言)

    名词 查询表 函数 行{}/列[] 单元格 表(Table) 列表(List) 记录(Record) 引用[查询表] =查询表表名 引用[应用的步骤] =步骤名 引用表中的[单元格](深化) =表{行 ...

  3. 【cs231n笔记】assignment1之KNN

    k-Nearest Neighbor (kNN) 练习 这篇博文是对cs231n课程assignment1的第一个问题KNN算法的完成,参考了一些网上的博客,不具有什么创造性,以个人学习笔记为目的发布 ...

  4. 小迪安全 Web安全 基础入门 - 第三天 - 抓包&封包&协议&APP&小程序&PC应用&WEB应用

    一.抓包工具 1.Fiddler.Fiddler是一个用于HTTP调试的代理服务器应用程序,能捕获HTTP和HTTPS流量,并将其记录下来供用户查看.它通过使用自签名证书实现中间人攻击来进行日志记录. ...

  5. CF1560D Make a Power of Two 题解

    Content 给定一个整数 \(n\).每次操作你可以做两件事情中的一件: 删去这个数中的一个数位(如果这个数只剩下一位,则可以把它删空). 在这个数的右边添加一个数位. 你可以以任意顺序执行无限次 ...

  6. 遇到奇葩的现象,给input的id为10的value属性赋值为6,但是怎么显示的时候值还是原先的9的

    遇到奇葩的现象,给input的id为10的value属性赋值为6,但是怎么显示的时候值还是原先的9的 后来发现原来是id标签重复了,所有以后得注意了. 人员信息input的id属性与隐藏input属性 ...

  7. JAVA生成订单编号工具类

    JAVA根据时间戳和随机数方式生成订单编号工具类 OrderUtils.java package com.util; import java.text.DateFormat; import java. ...

  8. 自动化中不能犯的4个RPA错误-RPA学习天地

    自动化在客户支持中的使用预计在未来几年会加速. 根据Dimension Research的数据,2022年72%的客户互动将通过机器人流程自动化(RPA)等新兴技术进行.电话互动将从41%下降到12% ...

  9. 【九度OJ】题目1024:畅通工程 解题报告

    [九度OJ]题目1024:畅通工程 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述: 省政府"畅 ...

  10. Tomcat 组成与工作原理

    开源的 Java Web 应用服务器,实现了 Java EE(Java Platform Enterprise Edition)的部分技术规范,比如 Java Servlet.Java Server ...