自分の星を使用して、カスタム スタイルを使用する評価バーをアプリに作成しました。
私の評価バーは次のように定義されています。
<RatingBar
android:layout_width="wrap_content"
android:layout_height="15dp"
android:id="@+id/ratingBar"
android:numStars="10"
android:stepSize="1"
android:rating="10"
android:layout_marginTop="5dp"
android:layout_below="@id/feedbackTable"
android:layout_toRightOf="@id/ratingText"
android:layout_marginLeft="5dp"
style="@style/starBar" />
私が styles.xml で使用するスタイルは次のとおりです。
<style name="starBar" parent="@android:style/Widget.RatingBar">
<item name="android:progressDrawable">@drawable/ratingstars</item>
<item name="android:minHeight">22dip</item>
<item name="android:maxHeight">22dip</item>
</style>
ratingstars.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/ratingstars_full_empty" />
<item android:id="@+id/secondaryProgress" android:drawable="@drawable/ratingstars_full_empty" />
<item android:id="@+id/progress" android:drawable="@drawable/ratingstars_full_filled" />
</layer-list>
android:rating を調整すると、プレビューに正しい量の星が表示され、空になっていることが示されます。ただし、エミュレーターまたは実際のデバイスを使用すると、バーは星で完全に満たされたままになります (10/10)。次のコードを使用して、プログラムで調整しようとしました。
ratingbar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
Log.d("rating", rating+"");
ratingbar.setRating(rating);
}
});
現在、logcat のログは実際に評価 IS が変更されたことを示していますが、バーは視覚的に更新されず、たとえば 6 つ星がいっぱいで 4 つが空で、理由がわかりません。
プレビューでも正しいことが示されているため、ドローアブルは正しいのですが、何が原因でしょうか?
編集:
これは ratingstars_full_filled.xml です
<?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/star" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="@drawable/star" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="@drawable/star" />
<item android:drawable="@drawable/star" />
</selector>
そしてここに ratingstars_full_empty.xml があります
<?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/star_empty" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="@drawable/star_empty" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="@drawable/star_empty" />
<item android:drawable="@drawable/star_empty" />
</selector>