覆蓋的actionbar

原文地址:https://developer.android.com/training/basics/actionbar/overlaying.html#EnableOverlay html

 覆蓋的actionbar        java

一般,actionbar看上去是在界面的上方,會稍微佔用了屏幕的可用空間。若是在某些設計中你想要隱藏和現實actionbar,能夠調用hide()和show()方法。而後這將致使你的界面要基於新的空間大小從新計算和繪製。android

        爲了不在actionbar隱藏和顯示的時候從新計算佈局大小,你能夠使用actionbar的overlay模式 。當使用這個模式時,你的界面佈局能使用全部的可見空間,就好像沒有actionbar同樣。而actionbar會繪製在界面的前面。他會遮擋住界面上方的一些部分,可是當actionbar隱藏或顯示時,你不須要從新計算佈局。
app

        若是你但願你的佈局在actionbar下面也能部分顯示,那就給actionbar建立一個背景部分透明的自定義樣式。ide

使用Overlay模式佈局

        使用OVerlay模式,須要建立一個自定義的主題繼承已存在的actionbar主題,而且設置android:windowActionBarOverlay屬性爲truespa

針對Android3.0及更高版本設計

若是最小版本設置爲11或是更高,你自定義的主題須要使用Theme.Holo主題或是他的繼承類做爲你自定義主題的父類。例如:
code

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

針對Android2.1和更高版本
xml

 若是你的應用使用支持包來兼容運行在低於Android3.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>

同時要注意的是,這個主題包含了兩種定義windowActionBarOverlay樣式的方式,一種有android:前綴一個沒有,有前綴的是針對有該樣式的android版本,沒有前綴的是針對舊的版本使用支持包來讀取樣式。

指定佈局的頭部間距

當actionbar在overlay模式下,部分被隱藏的佈局也想要可見,能夠指定佈局的頭部margin或padding爲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>

若是使用支持庫的actionbar

<!-- 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>
相關文章
相關標籤/搜索