Android学习路线(十)如何将Action Bar叠放在你的布局上

为了第六篇的完整性,这里先贴出原文,下次再来翻译;:p

原文地址:http://developer.android.com/training/basics/actionbar/overlaying.html

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.

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

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.

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.

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.

For Android 3.0 and higher only

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:

<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

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:

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

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:

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

Android学习路线(十)如何将Action Bar叠放在你的布局上

时间: 2024-10-31 13:01:30

Android学习路线(十)如何将Action Bar叠放在你的布局上的相关文章

Android学习路线(六)为Android应用添加ActionBar

Action bar 是你可以为你的应用的Activity实现的最为重要的设计元素之一.它提供了集中UI特性,并且通过提供和其他的Android应用的一致性体验让你的应用能够很快被用户熟悉.主要的功能包括: 一个专用的显示应用表示的地方,并且能够指出用户当前在应用中的位置. 用户能够很方便地访问重要的功能(例如搜索). 提供视图切换导航(通过tab,或者下拉列表). 这次的训练课程提供了对action bar基础知识的快速引导.有关action bar的各种特性的更多信息,请查看Action B

Android学习路线(七)设置Action Bar

在action bar最今本的形式中,它仅仅在左边展示了activity的标题以及应用的icon.即使在这种简单的形式中,它也只是告诉用户现在在应用的哪个activity中,同时为你的应用保持一个标识. 图1. 一个展示应用icon和activity标题的action bar 设置一个基本的action bar需要你的应用使用支持action bar的主题.如何来请求这样的主题要看你所选择的你的应用最低支持的Android版本.因此本课会根据应用支持的不同的Android最低版本分为两个部分.

Android学习路线(八)为Action bar添加action按钮

Action bar允许你为与当前应用上下文相关的最重要的action items添加action按钮.那些直接显示在action bar上的icon或者文字都被称作action buttons.那些不适合action bar或者不是那么重要的Actions将会被隐藏在action overflow(译者注:action bar最右侧的垂直的三个点)里. 图1. 一个包含Search功能的action button和用来展示附加action的action overflow. 在XML文件中指定A

Android学习路线(九)为Action Bar添加Style

这里先贴出原文,下次再来翻译::p 原文地址:http://developer.android.com/training/basics/actionbar/styling.html The action bar provides your users a familiar and predictable way to perform actions and navigate your app, but that doesn't mean it needs to look exactly the

Android学习路线(二十四)ActionBar Fragment运用最佳实践

通过前面的几篇博客,大家看到了Google是如何解释action bar和fragment以及推荐的用法.俗话说没有demo的博客不是好博客,下面我会介绍一下action bar和fragment在实战中的应用,以及相关demo源码,希望和大家相互交流. 了解过fragment的同学们应该都知道,fragment是android 3.0版本才出现的的,因此如果要在支持android 3.0一下版本的工程中使用fragment的话是需要添加Support Library的.具体如何添加我就不再赘述

Android学习路线(十二)Activity生命周期——启动一个Activity

先占个位置,过会儿来翻译,:p Unlike other programming paradigms in which apps are launched with a main()method, the Android system initiates code in an Activity instance by invoking specific callback methods that correspond to specific stages of its lifecycle. Th

Android学习路线(十九)支持不同设备——支持不同(Android)平台版本

当最新的Android版本为你的应用提供着很棒的APIs时,你却要在更多的设备更新之前继续支持老的系统版本.这篇课程如何在继续支持低版本的系统的情况下使用新版本的高级API. Platform Versions 仪表板展示了最新的活跃设备上运行的Android系统版本的分布,基于设备访问Google Play商店的次数.通常情况下,支持90%的活跃设备同时使用最新版本作为target是一个好习惯. 贴士: 为了在不同的Android版本上提供最好的特性和功能,你应该在你的应用中使用Android

Android学习路线(十四)Activity生命周期——停止和重启(Stopping and Restarting)一个Activity

先占个位置,下次翻译~ :p Properly stopping and restarting your activity is an important process in the activity lifecycle that ensures your users perceive that your app is always alive and doesn't lose their progress. There are a few of key scenarios in which

Android学习路线(二十)运用Fragment构建动态UI

要在Android系统上创建一个动态或者多面板的用户界面,你需要将UI组件以及activity行为封装成模块,让它能够在你的activity中灵活地切换显示与隐藏.你可以使用Fragment类来创建这些模块,它们能够表现得有些像嵌套的activity,它们定义着自己的布局,管理自己的生命周期. 当一个fragment指定了它自己的布局,它可以在activity中和其他的fragment配置为不同的组合,这样就能够为不同的屏幕尺寸来修改你的布局配置(在小屏幕上一次展现一个fragment,而在大屏