Android RatingBar 无法设置评分
Android RatingBar can't set rating
我正在尝试在我的 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
你发小了mistake/typo
您将不得不使用 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>
我正在尝试在我的 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
你发小了mistake/typo 您将不得不使用 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>