【Android介面實現】Overlaying the Action Bar,androidoverlaying

來源:互聯網
上載者:User

【Android介面實現】Overlaying the Action Bar,androidoverlaying

    轉載請註明出處:http://blog.csdn.net/zhaokaiqiang1992

    本篇文章翻譯自http://developer.android.com/training/basics/actionbar/overlaying.html,想查看原文的同學可以自己翻牆看。

    預設的,ActionBar會出現在你的Activity的視窗上面,這樣可能會減少剩下的Activity的可見地區的大小。如果,在使用者的互動的過程中,你想要隱藏或者是展示ActionBar,你可以通過hide()或者是show()方法來控制ActionBar的顯示與否。然而,這會導致你的Activity根據新的面積重新計算和繪製布局。

    為了避免當ActionBar的顯示狀態發生改變的時候也改變布局,你可以通過設定ActionBar的overlay模式來達到這個目的。當我們使用overlay模式的時候,我們的activity可以使用所有的位置,這是因為系統在繪製ActionBar的時候,是直接繪製在布局上面的。這樣掩蓋了布局的頂部,但是當ActionBar顯示或者是隱藏的時候,系統不需要重新計算布局,在過渡的時候也是一樣的。

    小提示:如果你想你的布局在ActionBar的後面是部分可見的,也就是有點透明的效果,那麼你需要建立一個自訂一個ActionBar的style,然後設定一個透明的背景,關於設定自訂ActionBar背景的操作,請參考style the action bar.

    


    啟動Overlay模式

    如果我們需要開啟Overlay模式,那麼我們需要繼承一個已有的ActionBar的theme,然後自訂 android:windowActionBarOverlay屬性為true。

    相容3.0以上版本

    如果要相容這個版本,那麼我們可以使用下面的代碼

<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.1以上的版本,並且使用版本相容庫的話,那麼我們就必須自訂一個繼承自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>

    需要注意的是,如果要相容2.1,那麼我們需要定義兩個item屬性,一個是帶有android:的,一個是不帶的,分別對應非相容庫和相容庫的。


    如果我們使用了overlay模式之後,在Activity上原本應該顯示的位置的內容可能會被遮擋,為瞭解決這個問題,我們可以設定父布局的padding或者是margen屬性,防止內容被遮擋,下面的程式碼完成了這個功能:

<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:attr/actionBarSize是一個固定值。如果我們要支援低版本的話,那麼應該把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>

    在這種情況下,不管是在高版本還是低版本,都能夠正常的工作。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.