Overlaying the Action Bar --1.1.4

来源:互联网 发布:前端请求数据 编辑:程序博客网 时间:2024/05/24 15:41

默认情况下,Action bar 只占用一小部分的屏幕尺寸。如果在和用户的交互过程中,你想隐藏或者显示 Action bar ,可以通过调用hide()或者show()函数实现。但是这样做,将会导致 Activity 重新计算当前屏幕的尺寸,并且根据当前屏幕的尺寸重绘。

为了在隐藏和显示 Action bar 时,不让 Activity 重新计算当前屏幕的尺寸,你可以给 Action bar 设置 overlay mode。这样做之后,你的 Activity 将占用整个屏幕,此时的 Action bar 和 Activity 不在一个维度,所以在调用 Action bar 的 hide()或者show()函数时,Activity 不会重新计算当前屏幕的尺寸,切换也会显得更加自然。

提示:如果你想创建一个半透明的 Action bar ,那你只需要为 Action bar 设置一个特定的背景可以了。

这里写图片描述

1. Enable Overlay Mode

为了给 Action bar 添加 overlay mode,你需要自定义一个继承系统主题的主题,并且设置 android:windowActionBarOverlaytrue

1. For Android 3.0 and higher only

当你为 Android 版本为 11 及以上添加 Action bar 时,你可以像下面这样做:

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

2. For Android 2.1 and higher

如果你的 Android 版本不是 3.0,而是 2.1,而且想达到上面同样的效果,你可以这样:

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

2. Specify Layout Top-margin

当你为 Action bar 设置 overlay mode之后,它便会遮盖住你 Activity 的一部分,因此如果你不想让 Action bar 你的 Activity,你可以 Activity 的布局文件添加 Margin或者Padding,像下面这样:

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

如果你的 Android 版本不是 3.0,而是 2.1,而且想达到上面同样的效果,你可以这样:

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

好了,这篇文章到这里就结束了,希望能帮到小伙伴,have a good day~

1 0