首页 > 代码库 > Android官方入门文档[5]建立操作栏

Android官方入门文档[5]建立操作栏

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]建立操作栏