自定义RatingBar未显示超过1星

时间:2016-12-20 07:54:49

标签: android android-layout android-xml ratingbar

我已经实施了this stackoverflow thread的自定义评级栏,即使我将 numStars 设置为5或任何其他数字

,也不会显示多个星标

这是守则。

我将矢量绘图用于图像。

main_activity.xml

中的RatingBar
<RatingBar
        android:id="@+id/ratingBarValue"
        style="@style/Fuel_Indicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:numStars="5"
        android:stepSize="1.0"
        android:max="5" />
样式中的

Fuel_Indicator 样式。

<style name="Fuel_Indicator" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/fuel_bar_full</item>
    <item name="android:indeterminateDrawable">@drawable/fuel_bar_full</item>
    <item name="android:minHeight">23dip</item>
    <item name="android:maxHeight">25dip</item>
</style>

fuel_bar_filled.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background"
    android:drawable="@drawable/fuel_bar_empty" />
<item android:id="@android:id/secondaryProgress"
    android:drawable="@drawable/fuel_bar_empty" />
<item android:id="@android:id/progress"
    android:drawable="@drawable/fuel_bar_filled" />

fuel_bar_empty.xml

<selector
xmlns:android="http://schemas.android.com/apk/res/android">`
<item android:state_pressed="true"
    android:state_window_focused="true"
    android:drawable="@drawable/ic_fuel_empty" />

<item android:state_focused="true"
    android:state_window_focused="true"
    android:drawable="@drawable/ic_fuel_empty" />

<item android:state_selected="true"
    android:state_window_focused="true"
    android:drawable="@drawable/ic_fuel_empty" />

<item android:drawable="@drawable/ic_fuel_empty" />
</selector>

fuel_bar_filled.xml

<selector
xmlns:android="http://schemas.android.com/apk/res/android">`

<item android:state_pressed="true"
    android:state_window_focused="true"
    android:drawable="@drawable/ic_fuel_full" />

<item android:state_focused="true"
    android:state_window_focused="true"
    android:drawable="@drawable/ic_fuel_full" />

<item android:state_selected="true"
    android:state_window_focused="true"
    android:drawable="@drawable/ic_fuel_full" />

<item android:drawable="@drawable/ic_fuel_full" />

</selector>

1 个答案:

答案 0 :(得分:22)

您的实施是完美的但是您正在使用导致此问题的Vector Drawable。因此,要使用它,请尝试将Vector Drawable替换为drawable文件夹中的png图像,并查看其工作的魔力。

您正在Vector Drawable使用layer-list,因此您的Vector Drawable图片适合整个布局,请尝试将其替换为png,以解决您的问题。