如何使用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 a library project based on the support library code:

  1. Make sure you have downloaded the Android Support Library using the SDK Manager.
  2. Create a library project and ensure the required JAR files are 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 you are adding theappcompat 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. In the new library project, expand the libs/ folder, right-click each .jar file and select Build Path > Add to Build Path. For example, when creating the the v7 appcompat project, add both the android-support-v4.jar andandroid-support-v7-appcompat.jar files to the 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 are available to projects that depend on this library project. For example, the appcompat project requires you 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 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 select Properties.
  2. In the Library pane, click Add.
  3. Select the library project and click OK. For example, the appcompat project should be listed as android-support-v7-appcompat.
  4. In the properties window, click OK.

Once your project is 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:
    <span class="tag" style="color:#0088;"><activity</span><span class="pln" style="color:#000000;"> </span><span class="atn" style="color:#882288;">android:theme</span><span class="pun" style="color:#66660;">=</span><span class="atv" style="color:#0880;">"@style/Theme.AppCompat.Light"</span><span class="pln" style="color:#000000;"> ... </span><span class="tag" style="color:#0088;">></span>

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 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 higher. If you don't want the action bar for an activity, set the activity theme to Theme.Holo.NoActionBar.

以上摘自Android官網。


示例代碼:

[html] view plaincopy
  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.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="17" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:theme="@style/Theme.Base.AppCompat.Light"  
  18.             android:name="com.folyd.actionbartest.MainActivity"  
  19.             android:label="@string/app_name" >  
  20.             <intent-filter>  
  21.                 <action android:name="android.intent.action.MAIN" />  
  22.   
  23.                 <category android:name="android.intent.category.LAUNCHER" />  
  24.             </intent-filter>  
  25.         </activity>  
  26.     </application>  
  27.   
  28. </manifest>  




[java] view plaincopy
  1. package com.folyd.actionbartest;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v7.app.ActionBar;  
  5. import android.support.v7.app.ActionBarActivity;  
  6.   
  7. public class MainActivity extends ActionBarActivity {  
  8.     private ActionBar actionBar;  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_main);  
  14.         actionBar = getSupportActionBar();  
  15.         actionBar.setDisplayShowHomeEnabled(true);  
  16.     }  
  17.   
  18. }  

效果截圖:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章