简体   繁体   English

Android RatingBar无法设置评分

[英]Android RatingBar can't set rating

I'm trying to implement a RatingBar in my Android app but I'm running into some issues when trying to set the rating. 我正在尝试在我的Android应用中实现RatingBar,但是在尝试设置评分时遇到了一些问题。 My bar is always displayed as having the maximum available rating. 我的酒吧始终显示为具有最高可用评分。 Below are my files: 以下是我的文件:

styles.xml styles.xml

<style name="ratingBarStyling" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/star_rating_bar</item>
    <item name="android:indeterminateDrawable">@drawable/star_rating_bar</item>
    <item name="android:minHeight">25dip</item>
    <item name="android:maxHeight">27dip</item>
</style>

(same for v21) (与v21相同)

star_rating_bar.xml star_rating_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/background"
    android:drawable="@drawable/star_rating_bar_full_empty" />
<item android:id="@+id/secondaryProgress"
    android:drawable="@drawable/star_rating_bar_full_empty" />
<item android:id="@+id/progress"
    android:drawable="@drawable/star_rating_bar_full_filled" />
</layer-list>

star_rating_bar_full_empty star_rating_bar_full_empty

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">

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

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

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

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

</selector>

star_rating_bar_full_filled star_rating_bar_full_filled

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">

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

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

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

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

</selector>

And finally the implementation of the RatingBar on the Fragment layout: 最后是Fragment布局上的RatingBar的实现:

<RatingBar
        style="@style/ratingBarStyling"
        android:id="@+id/venue_card_rating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:numStars="5"
        android:stepSize="1.0"
        android:rating="2.0"/>

Now for some reason I can see the filled star icons but non of the empty ones. 现在由于某种原因,我可以看到填充的星形图标,但看不到空的星形图标。 I have also tried setting it programmatically but the results were the same. 我也尝试过以编程方式设置它,但是结果是相同的。 Any help would be greatly appreciated. 任何帮助将不胜感激。

This is what I'm always getting: stars.png 这就是我一直得到的: stars.png

You have made small mistake/typo You will have to use android:id instead of providing a new id. 您犯了小错误/输入错误,您将不得不使用android:id而不是提供新的ID。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background"
        android:drawable="@drawable/star_rating_bar_full_empty" />
    <item android:id="@android:id/secondaryProgress"
        android:drawable="@drawable/star_rating_bar_full_empty" />
    <item android:id="@android:id/progress"
        android:drawable="@drawable/star_rating_bar_full_filled" />
</layer-list>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM