我试图在我的Android应用中实施RatingBar,但在尝试设置评级时我遇到了一些问题。我的栏总是显示为具有最大可用评级。以下是我的文件:
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>
(v21相同)
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
<?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
<?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>
最后在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"/>
现在由于某种原因,我可以看到填充的星形图标,但不是空的图标。我也试过以编程方式设置它,但结果是一样的。任何帮助将不胜感激。
这就是我一直得到的:stars.png
答案 0 :(得分:0)
你犯了小错误/拼写错误 您将不得不使用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>