我創建了一個自定義評級欄。我設置了5個自定義星形圖像,但它只顯示一個。不管數量是多少,我只能看到一個,不管星星的數量多少。任何幫助將不勝感激。
xml.code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<RatingBar
android:id="@+id/ratingbar_default"
style="@style/RatingBar"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginBottom="20dp" />
<TextView
android:id="@+id/textView"
android:layout_width="276dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="show state up checkbox"
android:textColor="#CC00CC"
android:textSize="20dp" />
Java代碼:
Activity RatingBarActivity
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rating);
final TextView text = (TextView) findViewById(R.id.textView);
final RatingBar ratingBar_default = (RatingBar) findViewById(R.id.ratingbar_default);
ratingBar_default.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener()
{
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser)
{
// TODO Auto-generated method stub
text.setText("Rating: " + String.valueOf(rating));
}
});
<?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_ratingbar_full_empty" />
<item
android:id="@android:id/secondaryProgress"
android:drawable="@drawable/star_ratingbar_full_empty" />
<item
android:id="@android:id/progress"
android:drawable="@drawable/star_ratingbar_full_filled" />
</layer-list>
RES /抽拉/ star_ratingbar_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/star2" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="@drawable/star2" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="@drawable/star2" />
<item android:drawable="@drawable/star2" />
</selector>
RES/drawabstar_ratingbar_full_filled.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- This is the rating bar drawable that is used to
show a unfilled cookie. -->
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="@drawable/star3" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="@drawable/star1" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="@drawable/star1" />
<item android:drawable="@drawable/star1" />
</selector>
styles.xml文件:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"></style>
<style name="RatingBar" parent="@android:style/Widget.RatingBar">
<item name="android:progressDrawable">@drawable/star_rating_bar_full</item>
<item name="android:minHeight">48dip</item>
<item name="android:maxHeight">48dip</item>
</style>
</resources>
參考http://stackoverflow.com/questions/30753688/android-ratingbar-below-lollipop-renders-incorrectly/ –