`
king_tt
  • 浏览: 2108409 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

如何使用android-support-V7包中ActionBar(Eclipse版)

 
阅读更多

以前3.0以前的版本要使用ActionBar,必须使用国外大牛写的ActionBarSherlock这个开源项目。今年的Google/IO大会之后,Google官方在android-support-v7包中添加了ActionBar,开始让2.1以后的版本支持ActionBar,从此以后曾经最火的Android开源项目ActionBarSherlock可以退出历史舞台了。


要是用V7包中ActionBar也很简单,但有一个需要注意的地方。有些人可能刚开始仅仅是把android-support-v7-appcompat.jar导入项目中,但是在设置Activity的theme时会报错,提示找不到"@style/Theme.AppCompat"。这是由于我们要把v7和资源文件一起导入才行。

具体使用步骤(针对于Eclipse):

Create alibrary projectbased on the support library code:

  1. Make sure you have downloaded theAndroid Support Libraryusing theSDK Manager.
  2. Create a library project and ensure the required JAR files are included in the project's build path:
    1. SelectFile > Import.
    2. SelectExisting Android Code Into Workspaceand clickNext.
    3. Browse to the SDK installation directory and then to the Support Library folder. For example, if you are adding theappcompatproject, browse to<sdk>/extras/android/support/v7/appcompat/.
    4. ClickFinishto import the project. For the v7 appcompat project, you should now see a new project titledandroid-support-v7-appcompat.
    5. In the new library project, expand thelibs/folder, right-click each.jarfile and selectBuild Path > Add to Build Path. For example, when creating the the v7 appcompat project, add both theandroid-support-v4.jarandandroid-support-v7-appcompat.jarfiles to the build path.
    6. Right-click the project and selectBuild Path > Configure Build Path.
    7. In theOrder and Exporttab, check the.jarfiles you just added to the build path, so they are available to projects that depend on this library project. For example, theappcompatproject requires you to export both theandroid-support-v4.jarandandroid-support-v7-appcompat.jarfiles.
    8. UncheckAndroid Dependencies.
    9. ClickOKto complete the changes.

You now have a library project for your selected Support Library that 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 selectProperties.
  2. In the Library pane, clickAdd.
  3. Select the library project and clickOK. For example, theappcompatproject should be listed asandroid-support-v7-appcompat.
  4. In the properties window, clickOK.

Once your project is set up with the support library, here's how to add the action bar:

  1. Create your activity by extendingActionBarActivity.
  2. Use (or extend) one of theTheme.AppCompatthemes for your activity. For example:
    <activity android:theme="@style/Theme.AppCompat.Light" ... >

Now your activity includes the action bar when running on Android 2.1 (API level 7) or higher.

On API level 11 or higher

The action bar is included in all activities that use theTheme.Holotheme (or one of its descendants), which is the default theme when either thetargetSdkVersionorminSdkVersionattribute is set to"11"or higher. If you don't want the action bar for an activity, set the activity theme toTheme.Holo.NoActionBar.

以上摘自Android官网。


示例代码:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns: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="17"/>
  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. <actionandroid:name="android.intent.action.MAIN"/>
  20. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  21. </intent-filter>
  22. </activity>
  23. </application>
  24. </manifest>




  1. packagecom.folyd.actionbartest;
  2. importandroid.os.Bundle;
  3. importandroid.support.v7.app.ActionBar;
  4. importandroid.support.v7.app.ActionBarActivity;
  5. publicclassMainActivityextendsActionBarActivity{
  6. privateActionBaractionBar;
  7. @Override
  8. protectedvoidonCreate(BundlesavedInstanceState){
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. actionBar=getSupportActionBar();
  12. actionBar.setDisplayShowHomeEnabled(true);
  13. }
  14. }

效果截图:

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics