注:本文翻譯自Google官方的Android Developers Training文檔,譯者技術一般,由于喜愛安卓而產生了翻譯的念頭,純屬個人興趣愛好。
原文鏈接: http://developer.android.com/training/basics/actionbar/overlaying.html
默認的,Acton Bar會顯示在你的activity窗口的上部,使得它會稍許減少了activity布局的剩余空間。如果在用戶的UI交互過程中,你希望可以隱藏和顯示Action Bar,你可以通過調用其
hide()
和
show()
這兩個方法來實現。然而,這會導致你的
activity
在新尺寸的基礎上重新計算和繪制布局。
為了避免在Action Bar隱藏和顯示的時候重新調整布局的尺寸,你可以使用Action Bar的覆蓋模式( overlay mode )。當在 覆蓋模式 中,你的activity會使用屏幕上所有的空間,就好像action bar不在那兒一樣,然后系統會將action bar繪制于你的布局之上。這會使得布局中頂部的一部分被遮蔽,但是現在這個時候對action bar進行隱藏或者顯示的時候,系統就不需要重新調整布局的尺寸,使得畫面過渡平滑而自然。
Tip:
如果你希望你的布局在你的Action Bar之后是部分可見的,可以為action bar創建一個半透明的自定義背景,如圖1所示。關于如何自定義Action Bar的風格,可以閱讀: Styling the Action Bar 。(博客鏈接: http://www.cnblogs.com/jdneo/p/3444484.html )
圖1. Gallery的覆蓋于布局之上(overlay mode)的Action Bar
?
一). 啟用覆蓋模式
為了啟用Action Bar的覆蓋模式,你需要創建一個繼承自一個存在了的action bar主題的自定義主題,然后將“ android:windowActionBarOverlay ”這一屬性字段設置為“ true ”。
對于Android 3.0或更高
如果你的
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 >
對于Android 2.1或更高
如果你的應用使用“ 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 >
注意到這個主題包含了“
windowActionBarOverlay
”風格的兩處定義:一個有“
android:
”前綴而另一個沒有。有“
android:
”前綴的版本適用于包含了這個風格的Android平臺,而沒有的版本適用于那些較古老的版本,風格從“
Support Library
”中獲取。
?
二). 指定布局的頂部邊緣
當Action Bar在覆蓋模式時,它可能會遮蔽你的一部分本應該要可見的布局。為了保證這些元素永遠處于action bar的下方,可以通過添加頂部View的外邊距( 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 >
如果你使用的是“ 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 >
在這個例子中,“ ?attr/actionBarSize ”這個沒有前綴的值適用于所有版本,包括Android 3.0和更高版本。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
