標籤:android應用 android應用android開發
Android官方入門文檔[8]重疊操作欄 Overlaying the Action Bar
重疊操作欄
This lesson teaches you to
1.Enable Overlay Mode
1.For Android 3.0 and higher only
2.For Android 2.1 and higher
2.Specify Layout Top-margin
You should also read
?Styles and Themes
這節課教你
1.啟用重疊模式
1.對Android的3.0和更高版本
2.對於安卓2.1及以上
2.指定布局頂邊距
你也應該閱讀
?樣式和主題
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.
預設情況下,操作欄會出現在你的使用中視窗的頂部,略有減少了可用空間為您的活動布局的其餘部分的金額。如果,使用者互動的過程中,你要隱藏和顯示操作欄,您可以通過調用hide()和show()在動作欄來做。但是,這會導致你的活動重新計算和重繪基於新的大小布局。
Figure 1. Gallery‘s action bar in overlay mode.
圖1.重疊模式中Gallery畫廊的操作欄。
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.
提示:如果你希望你的布局是操作欄的後面部分可見,建立操作欄自訂樣式與部分透明的背景,比如1所示。有關如何定義操作欄的背景資料之一,閱讀樣式化的操作欄。
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.
要啟用重疊模式操作欄中,你需要建立一個自訂佈景主題,擴充現有的操作欄的主題,並設定是Android:為true windowActionBarOverlay屬性。
For Android 3.0 and higher only
針對Android3.0和更高版本
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:
如果你的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>
For Android 2.1 and higher
針對Android2.1及更高
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:
如果您的應用程式使用的是支援庫的相容性上啟動並執行版本,比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>
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.
還要注意的是這個主題包含兩個定義為windowActionBarOverlay樣式化:一個是Android:首碼,一個沒有。一個與Android:首碼是包含風格的平台和一個沒有首碼是舊版本,上面寫著從支援庫風格的Android版本。
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:
當動作欄為疊加模式,它可能會掩蓋一些你的布局應該保持可見。為了確保這些項目仍然低於操作欄在任何時候,無論是增加保證金或填充,以利用actionBarSize指定的高度view的頂部。例如:
<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:
如果您使用的是支援庫的操作欄中,您需要刪除的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>
In this case, the ?attr/actionBarSize value without the prefix works on all versions, including Android 3.0 and higher.
在這種情況下,如果沒有首碼,?attr/actionBarSize值適用於所有的版本中,包括Android3.0和更高。
Next class: Supporting Different Devices
下一個課程:支援不同裝置
本文翻譯自:https://developer.android.com/training/basics/actionbar/overlaying.html
Android官方入門文檔[8]重疊操作欄