http://blog.csdn.net/appte/article/details/11712591
Prior to the previous 3.0 version to use Actionbar, must use the foreign Daniel wrote actionbarsherlock this open source project. After this year's Google/io conference, Google officially added Actionbar to the ANDROID-SUPPORT-V7 package, Started to let the 2.1 version support Actionbar, since then the most popular Android open source project Actionbarsherlock can exit the historical stage.
It is easy to use the V7 bag in Actionbar, but there is one place to pay attention. Some people may have just started to import Android-support-v7-appcompat.jar into the project, but will error when setting activity Theme, indicating "@style/theme.appcompat" is not found. This is because we are going to import V7 and resource files together.
Specific steps to use (for Eclipse):
Create a library project based on the Support library code:
- Make sure has downloaded the Android support Library using the SDK Manager.
- Create a library project and ensure the required JAR files is included in the project ' s build path:
- Select File > Import.
- Select Existing Android Code into Workspace and click Next.
- Browse to the SDK installation directory and then to the support Library folder. For example, if is adding the
appcompat
project, browse to <sdk>/extras/android/support/v7/appcompat/
.
- Click Finish to import the project. For the V7 AppCompat project, you should now see a new project titled Android-support-v7-appcompat.
- The new library project, expand
libs/
the folder, right-click each .jar
file and select Build Path > Add to Bui LD Path. For example, when creating the V7 AppCompat project, add both the and android-support-v4.jar
files to the android-support-v7-appcompat.jar
build path.
- Right-click the project and select Build path > Configure build Path.
- In the Order and Export tab, check the
.jar
files you just added to the build path, so they is available to Pro Jects that depend in this library project. For example, the appcompat
project requires your to export both the android-support-v4.jar
and android-support-v7-appcompat.jar
files.
- Uncheck Android Dependencies.
- Click OK to complete the changes.
You are now having a library project for the your selected support library so you can use with one or more application projects.
ADD the library to your application project:
- In the project Explorer, right-click your project and select Properties.
- In the Library pane, click Add.
- Select the Library project and click OK. For example, the
appcompat
project should is listed as Android-support-v7-appcompat.
- In the Properties window, click OK.
Once your project is a set up with the support library, here's how to add the Action bar:
- Create your activity by extending
ActionBarActivity
.
- Use (or extend) one of the
Theme.AppCompat
themes for your activity. For example:<activityandroid:theme="@style/theme.appcompat.light">
Now your activity includes the action Bar is running on Android 2.1 (API level 7) or higher.
On API level one or higher
The action Bar is included with all activities Theme.Holo
so use the theme (or one of it descendants), which is the default th The EME when either the targetSdkVersion
or minSdkVersion
attribute are set to "11"
or higher. If you don't want the action bar for a activity, set the activity theme to Theme.Holo.NoActionBar
.
The above is from the Android website.
Example code:
[HTML]View Plaincopyprint?
- <? XML version= "1.0" encoding="Utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="Com.folyd.actionbartest"
- android:versioncode="1"
- Android:versionname="1.0" >
- <uses-sdk
- android:minsdkversion="8"
- android:targetsdkversion=" /> "
- <application
- android:allowbackup="true"
- android:icon="@drawable/ic_launcher"
- Android:label="@string/app_name"
- Android:theme="@style/apptheme" >
- <activity
- Android:theme="@style/theme.base.appcompat.light"
- Android:name="Com.folyd.actionbartest.MainActivity"
- Android:label="@string/app_name" >
- <intent-filter>
- <action android:name="Android.intent.action.MAIN" />
- <category android:name="Android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </Application>
- </manifest>
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.folyd.actionbartest "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/>" <application android:allowbackup = "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/A Pptheme "> <activity android:theme=" @style/theme.base.appcompat.light "Android:name=" Co M.folyd.actionbartest.mainactivity "android:label=" @string/app_name "> <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= "android.in Tent.category.LAUNCHER "/> </intent-filter> </activity> </application></manif Est>
[Java]View Plaincopyprint?
- Package com.folyd.actionbartest;
- Import Android.os.Bundle;
- Import Android.support.v7.app.ActionBar;
- Import android.support.v7.app.ActionBarActivity;
- Public class Mainactivity extends Actionbaractivity {
- private ActionBar ActionBar;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- ActionBar = Getsupportactionbar ();
- Actionbar.setdisplayshowhomeenabled (true);
- }
- }
Package Com.folyd.actionbartest;import Android.os.bundle;import Android.support.v7.app.actionbar;import Android.support.v7.app.actionbaractivity;public class Mainactivity extends Actionbaractivity {private ActionBar ActionBar; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); ActionBar = Getsupportactionbar (); Actionbar.setdisplayshowhomeenabled ( true);}}
Effect:
Go to how to use V7 package in Actionbar (Eclipse edition)