標籤:android開發 android android應用 android 3.0 action bar
Android官方入門文檔[5]建立操作欄 Setting Up the Action Bar
建立操作欄
This lesson teaches you to
1.Support Android 3.0 and Above Only
2.Support Android 2.1 and Above
You should also read
?Setting Up the Support Library
這節課教你
1.僅支援Android3.0及以上
2.支援Android2.1及以上
你也應該閱讀
?設定支援庫
In its most basic form, the action bar displays the title for the activity and the app icon on the left. Even in this simple form, the action bar is useful for all activities to inform users about where they are and to maintain a consistent identity for your app.
在其最基本的形式中,操作欄顯示的活性和在左側的應用程式圖示的標題。即使在這種簡單的形式,操作欄是非常有用的所有活動,以告知使用者他們在哪裡,並保持你的應用程式一致。
Figure 1. An action bar with the app icon and activity title.
圖1.應用程式圖示和標題活動的操作欄。
Setting up a basic action bar requires that your app use an activity theme that enables the action bar. How to request such a theme depends on which version of Android is the lowest supported by your app. So this lesson is divided into two sections depending on which Android version is your lowest supported.
建立一個基本的操作欄需要你的應用程式使用的活動主題,使操作欄。如何申請這樣一個主題取決於哪個版本的Android是最低的了您的應用程式的支援。所以,這個課程是分為兩個部分,這取決於Android版本的最低支援。
Support Android 3.0 and Above Only
僅支援Android3.0及以上
--------------------------------------------------------------------------------
Beginning with Android 3.0 (API level 11), the action bar is included in all activities that use the Theme.Holo theme (or one of its descendants), which is the default theme when either the targetSdkVersion or minSdkVersion attribute is set to "11" or greater.
採用Android 3.0(API等級11)開始,操作欄包含在所有使用Theme.Holo主題(或它的子類),這是預設的主題時,無論是targetSdkVersion或的minSdkVersion屬性被設定為“11“或更大。
So to add the action bar to your activities, simply set either attribute to 11 or higher. For example:
所以在操作欄添加到您的活動,只需設定任一屬性為11或更高。例如:
<manifest ... >
<uses-sdk android:minSdkVersion="11" ... />
...
</manifest>
Note: If you‘ve created a custom theme, be sure it uses one of the Theme.Holo themes as its parent. For details, see Styling the Action Bar.
註:如果您已經建立了一個自訂佈景主題,可以肯定它採用的Theme.Holo主題之一作為其父類。有關詳細資料,請參見樣式的操作欄。
Now the Theme.Holo theme is applied to your app and all activities show the action bar. That‘s it.
現在Theme.Holo主題應用到您的應用程式的所有活動顯示操作欄。就是這樣。
Support Android 2.1 and Above
支援Android2.1及以上
--------------------------------------------------------------------------------
Adding the action bar when running on versions older than Android 3.0 (down to Android 2.1) requires that you include the Android Support Library in your application.
在舊比Android3.0(向下至Android2.1)版本上運行時,操作欄需要你在你的應用程式,包括Android的支援庫。
To get started, read the Support Library Setup document and set up the v7 appcompat library (once you‘ve downloaded the library package, follow the instructions for Adding libraries with resources).
Once you have the Support Library integrated with your app project:
要開始,請閱讀支援庫安裝檔案,並成立了V7 appcompat庫(一旦你下載的庫包,按照說明添加庫的資源)。
一旦你的支援庫整合到您的應用程式的項目:
1.Update your activity so that it extends ActionBarActivity. For example:
1.更新你的活動,使其擴充ActionBarActivity。例如:
public class MainActivity extends ActionBarActivity { ... }
2.In your manifest file, update either the <application> element or individual <activity> elements to use one of the Theme.AppCompat themes. For example: 2.在你的manifest檔案裡,更新無論是<application>元素或個人<活動>元素使用的Theme.AppCompat主題之一。例如
<activity android:theme="@style/Theme.AppCompat.Light" ... >
Note: If you‘ve created a custom theme, be sure it uses one of the Theme.AppCompat themes as its parent. For details, see Styling the Action Bar.
註:如果您已經建立了一個自訂佈景主題,可以肯定它採用的Theme.AppCompat主題之一作為其父類。有關詳細資料,請參見樣式的操作欄。
Now your activity includes the action bar when running on Android 2.1 (API level 7) or higher.
現在在Android2.1(API7級)或更高版本運行時,你的活動包括操作欄。
Remember to properly set your app‘s API level support in the manifest:
記住要正確設定你的應用程式的API層級支援manifest清單:
<manifest ... >
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="18" />
...
</manifest>
本文翻譯自:https://developer.android.com/training/basics/actionbar/setting-up.html
Android官方入門文檔[5]建立操作欄