設置 style="?android:attr/ratingBarStyleSmall" 這個屬性;聽說有效android
最近在寫一個程序用到了評星的RatingBar控件,發現了一個奇怪的問題,RatingBar在模擬器上看起來正常,裝到HTC g7的機子了就顯示不全,而且明顯感受圖像被拉申了(我用的是最小的style: ratingBarStyleSmall)。框架
覺得是本身的代碼問題,後來直接試了一下android的ApiDemos,發現也存在這個問題:佈局
<RatingBar android:id="@+id/small_ratingbar"ui
style="?android:attr/ratingBarStyleSmall"spa
android:layout_marginLeft="5dip"code
android:layout_width="wrap_content"orm
android:layout_height="wrap_content"xml
android:layout_gravity="center_vertical" />ip
如圖:get
在網上找了一些程序(如eoemarket之類的),發現他們都能在真機上正常顯示出來,而我把佈局文件改爲和他們如出一轍都不對。最後比來比去,在AndroidManifest.xml上加了一句:
<uses-sdk android:minSdkVersion="4" />
居然正確顯示了:
後來在網上也看到,這個貌似是一個公認的android框架的BUG。我也測了一下,發現<uses-sdk android:minSdkVersion="4" />,SdkVersion從4之後都是對的,3的話仍是以前的問題。Motorola Milestone 和 Nexus One 也存在一樣的問題。
我還不是很肯定靠成這個問題的緣由,因此付上一段一個老外的說明,你們能夠本身分析看:
I’ve just spent far too long wondering why the RatingBar views in my Android XML layouts look different on the emulators, HTC Hero and HTC Desire. Small style rating bars seemed to have no concept of their actual size and the large rating bars for user interaction were clipped and falling of the edge of the layouts.
The answer is that the RatingBar view doesn’t scale properly on high density screens if you have the build target set to Android 1.5. The fix is to target API level 4, add screen support declaration to the manifest and then be careful not to break anything for the 1/3 of users still on Android 1.5.
<supports-screens android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true" />
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="4" />