建立V7包庫(Library)項目以供其他項目引
- Chapter: 使用support-v7 ActionBar前的那些坑
以前3.0以前的版本要使用ActionBar,必須使用國外大牛寫的ActionBarSherlock這個開源項目。2013年的Google I/O大會之后,Google官方在android-support-v7包中添加了ActionBar,開始讓2.1以后的版本支持ActionBar,從此以后曾經最火的Android開源項目ActionBarSherlock可以退出歷史舞台了。
要是用v7包中ActionBar也很簡單,但有一個需要注意的地方。有些人可能剛開始僅僅是把android-support-v7-appcompat.jar導入項目中,但是在設置Activity的theme時會報錯,提示找不到"@style/Theme.AppCompat"。這是由於我們要把v7和資源文件一起導入才行,僅僅導入V7包是不夠的。
Eclipse下具體使用步驟為(摘自Android官網):
Create a library project based on the support library code:
- Make sure you have downloaded the Android Support Library using the SDK Manager.
- Create a library project and ensure the required JAR files are 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 you are adding the
appcompatproject, browse to/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.
- In the new library project, expand the
libs/folder, right-click each.jarfile and select Build 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. - Right-click the project and select Build Path > Configure Build Path.
- In the Order and Export tab, 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. - Uncheck Android Dependencies.
- 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:
- 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
appcompatproject should be listed as android-support-v7-appcompat. - In the properties window, click OK.
Once your project is 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.AppCompatthemes for your activity. For example:
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.
簡單地說,就是:
- 先升級SDK的Support Library;
- 以SDK目錄下/extras/android/support/v7/appcompat/為基礎新建一個項目;
- 將這個項目libs下的v4、v7包Build Path;
- Configure Build Path。
然后這個項目就可以被其它項目引用了。
2) v7庫項目themes_base.xml相關文件報錯問題
如果你用最新的SDK,可能新建的v7庫項目會有這樣的錯誤:
錯誤信息大概類似:
error: Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material'. error: Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.*' error: Error: No resource found that matches the given name: attr 'android:actionModeShareDrawable'.
解決辦法很簡單:
The only thing you have to do is to open the project.properties file of the android-support-v7-appcompat and change the target from target=android-19 to target=android-21. Afterwards just do a Project --> Clean... so that the changes take effect.
就是把編譯環境改成SDK 21就行了。兩種方法,打開project.properties,將target=android-7(或者其它)改成target=android-21;你也可以右鍵項目名,點擊Android,修改Project Build Target為Google APIs 21。
然后clean一下,項目報錯就會消失。
3) 關於引用Theme.AppCompat theme的報錯問題
當 v7 庫項目搭建好了,並且別的項目也引用了,你覺得應該沒什么錯誤,但是運行報錯了,大概報這樣的錯:
1 |
java.lang.RuntimeException: Unable to start activity ComponentInfo{jh.com.testandroidlayout/jh.com.testandroidlayout.navigator.NaviViewPagerActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) withthis activity. |
You need to use a Theme.AppCompat theme (or descendant) with this activity.
這錯誤很明顯,也很簡單,原來繼承自ActionBarActivity的類必須指定固定的集中Theme風格,而這些Theme風格是需要導入V7中的appcompat LIB庫工程,編譯后再引用才能引用使用。
打開你項目的AndroidManifest.xml,找到android:theme="@style/xxx",改成v7包里提供的方案就行了。比如:
1 |
android:theme="@style/Theme.AppCompat" 黑色系 |
2 |
android:theme="@style/Theme.AppCompat.Light" 淺色系 |
更多色彩方案可以自己在Application Tab頁去Browse。
stackoverflow 上有更詳盡的討論:http://stackoverflow.com/questions/21814825/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity
