How to use the V7 package in Actionbar (Eclipse edition)

Source: Internet
Author: User

Transferred from: 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:

  1. Make sure has downloaded the Android support Library using the SDK Manager.
  2. Create a library project and ensure the required JAR files is included in the project ' s build path:
    1. Select File > Import.
    2. Select Existing Android Code into Workspace and click Next.
    3. 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/ .
    4. Click Finish to import the project. For the V7 AppCompat project, you should now see a new project titled Android-support-v7-appcompat.
    5. 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.
    6. Right-click the project and select Build path > Configure build Path.
    7. 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.
    8. Uncheck Android Dependencies.
    9. 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:

    1. In the project Explorer, right-click your project and select Properties.
    2. In the Library pane, click Add.
    3. Select the Library project and click OK. For example, the appcompat project should is listed as Android-support-v7-appcompat.
    4. 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:

    1. Create your activity by extending ActionBarActivity .
    2. 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?
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="Com.folyd.actionbartest"
  4. android:versioncode="1"
  5. android:versionname="1.0" >
  6. <uses-sdk
  7. android:minsdkversion="8"
  8. android:targetsdkversion=" /> "
  9. <application
  10. android:allowbackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/apptheme" >
  14. <activity
  15. android:theme="@style/theme.base.appcompat.light"
  16. android:name="com.folyd.actionbartest.MainActivity"
  17. android:label="@string/app_name" >
  18. <intent-filter>
  19. <action android:name="Android.intent.action.MAIN" />
  20. <category android:name="Android.intent.category.LAUNCHER" />
  21. </intent-filter>
  22. </Activity>
  23. </Application>
  24. </manifest>





[Java]View Plaincopyprint?
  1. Package com.folyd.actionbartest;
  2. Import Android.os.Bundle;
  3. Import Android.support.v7.app.ActionBar;
  4. Import android.support.v7.app.ActionBarActivity;
  5. Public class Mainactivity extends Actionbaractivity {
  6. private ActionBar ActionBar;
  7. @Override
  8. protected void OnCreate (Bundle savedinstancestate) {
  9. super.oncreate (savedinstancestate);
  10. Setcontentview (R.layout.activity_main);
  11. ActionBar = Getsupportactionbar ();
  12. Actionbar.setdisplayshowhomeenabled (true);
  13. }
  14. }


Effect:

How to use the V7 package in Actionbar (Eclipse edition)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.