android 開發錦集之eclipse中appcompat_v7的作用

問題描述:

使用eclipse創建一個Android項目時,發現project列表中會多創建出一個appcompat_v7項目,再創建一個Android項目時,又會再多出一個appcompat_v7_2,如果再次創建,會以此類推地創建出appcompat_v7_x格式的“多餘項目”出來(此情況在ADT升級爲22.6.x版本後出現,22.3.x前的版本不會有)


查明原因:

ADT在22.3.x版本前沒有出現該情況,升級爲22.6.x版本後,纔出現該情況,可以猜測是新版本導致。猜測到原因後可以分析下appcompat_v7是用來做什麼的,展開appcompat_v7項目,會發現有一個readme.txt文件,雙擊查看,該文件描述如下:

Library Project including compatibility ActionBar.


This can be used by an Android project to provide
access to ActionBar on applications running on API 7+.


There is technically no source, but the src folder is necessary
to ensure that the build system works.  The content is actually
located in libs/android-support-v7-appcompat.jar.
The accompanying resources must also be included in the application.


字面意思大概可以看出,該項目是一個Library Project, 作用是讓你的Android Project 在API 7+的sdk版本(即Android2.1版本)可以訪問ActionBar的API,ActionBar相當於用戶界面的操作欄,具體ActionBar的解釋可以參考:http://developer.android.com/guide/topics/ui/actionbar.html。所以從中可以看出多出這個project是爲了兼容低版本的sdk,那我們創建的Android項目是如何引用這個類庫項目呢?通過右鍵點擊Android項目---->Properties-->Android,可以查看到該類庫項目已被引用進來了






原因已查明,可以驗證一下:

首先創建一個Android項目,在選擇Android版本號的界面,如果選擇的最低Android版本的"大版本號”小於編譯時採用的Android大版本號時,就需要考慮版本兼容問題,這樣子創建出項目後自然就會產生出appcompat_v7這個類庫項目出來。具體界面如下圖,如果編譯時採用4.3版本,選擇最低要求版本號爲2.x或者3.x時創建出來的項目就會自動創建appcompat_v7支持庫。




疑問:

可以不產生appcompat_v7這個類庫項目嗎?答案是可以的,只要版本號選擇時最低要求版本和編譯時用的版本選擇同一種即可,但不推薦這麼幹,開發Android程序需要考慮兼容多個版本


補充:

關於Library Project的相關內容,可以通過查看官方文檔介紹,具體查看:http://developer.android.com/tools/support-library/features.html#v4

找到 v7 appcompat library 的解釋:內容如下:

This library adds support for the Action Bar user interface design pattern.

Note: This library depends on the v4 Support Library. If you are using Ant or Eclipse, make sure you include the v4 Support Library as part of this library's classpath.

Here are a few of the key classes included in the v7 appcompat library:

  • ActionBar -  Provides an implementation of the action bar user interface pattern. For more information on using the Action Bar, see the Action Bar developer guide.
  • ActionBarActivity -  Adds an application activity class that must be used as a base class for activities that uses the Support Library action bar implementation.
  • ShareActionProvider -  Adds support for a standardized sharing action (such as email or posting to social applications) that can be included in an action bar.

This library is located in the <sdk>/extras/android/support/v7/appcompat/ directory after you download the Android Support Libraries. This library contains user interface resources. To include it in your application project, follow the instructions for adding libraries with resources.

The Gradle build script dependency identifier for this library is as follows:



以上是個人解決問題過程中查閱一些資料得出的一些結論,可能會有不對的地方,希望有看出問題的仁兄指點一二。剛開始學習Android開發,有些知識實現太薄弱,爲了記錄下平時遇到的一些問題,也只好先將自己理解的一些東西寫出來,等對Android多熟悉一些,再回來檢查下這些文章吧<( ̄︶ ̄)>

 

 

 

 

                                                                                                                                                   本文轉載自: http://blog.csdn.net/crazykbc/article/details/21553699

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