Android 自定義RatingBar控制元件,顯示不全問題
阿新 • • 發佈:2019-01-07
最近專案要用到 自定義RatingBar控制元件
但是自定義好了樣式發現,星星只顯示一半,於是在網上找各種解決方法。
最後竟然是直接把資源圖片,移動到較高解析度的資料夾裡面。
我的解決做法如下,如果有更好的方法,請留言告知。
一,先寫一個drawable,設定好background、secondaryProgress、progress
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <itemandroid:id="@android:id/background" android:drawable="@mipmap/recommend_star_disable"/> <item android:id="@android:id/secondaryProgress" android:drawable="@mipmap/recommend_star_normal"/> <item android:id="@android:id/progress" android:drawable="@mipmap/recommend_star_normal"/> </layer-list>
二、寫一個樣式
<!-- RatingBar樣式 --> <style name="RatingBar" parent="@android:style/Widget.RatingBar"> <item name="android:progressDrawable">@drawable/shape_rating_small</item> <item name="android:maxHeight">32dp</item> <item name="android:minHeight">32dp</item>我看網上說,圖片有多大,這裡maxHeight 、minHeight就設定多大。</style>
三、佈局xml 直接引用style
<RatingBar android:layout_width="wrap_content" android:numStars="5" style="@style/RatingBar" android:clickable="false" android:layout_height="16dp" android:layout_alignBottom="@+id/app_detail_icon" android:layout_alignLeft="@+id/textView" android:layout_alignStart="@+id/textView"/>
後來試了幾次,把資源圖片放在更高的解析度資料夾下,會顯示的更小。