2015-05-09 30 views
2

enter image description here自定義評分Bar僅顯示一顆星

我創建了一個自定義評級欄。我設置了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> 
+0

參考http://stackoverflow.com/questions/30753688/android-ratingbar-below-lollipop-renders-incorrectly/ –

回答

0

嘗試指定一個android:numStars屬性在佈局文件你的RatingBar activity_rating.xml。我想,默認值是1 - 這就是爲什麼你看到只有一顆星

+0

酷軟我已經嘗試過這種<的RatingBar android:numStars =「5」 android:progressDrawable =「@ drawable/ratingbar_selector」 android:stepSize =「0.2」/> –

+0

截圖上的綠色星星似乎是來自layer @android的full_empty星型:id/background 。它是否響應點擊? 我還看了@ drawable/ratingbar_full_holo_dark,發現它不使用選擇器。如果您使用靜態圖像而不是選擇器,您的代碼是否顯示所有星星? Holo評分欄還重新定義了indeterminateDrawable(與progressDrawable一起)。我想你也需要定義它。 –

-2

已經有一段時間,但從來沒有傷害的回答。 這是我的3步解決方案,無論SDK版本如何。

1添加爲你的XML佈局你的RatingBar:

<RatingBar 
    android:id="@+id/new_ratingbar" 
    android:progressDrawable="@drawable/custom_drawable" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:numStars="5" 
    android:stepSize="0.5"/> 

如果需要的話,你可以給的RatingBar和了minHeight或maxHeight根據需要添加其他屬性。

2 - 這裏是你將參考上述的機器人繪製:progressDrawable到:

custom_drawable.xml

<?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/rating_bar_empty" /> <!--this is your .png file--> 
    <item 
     android:id="@android:id/secondaryProgress" 
     android:drawable="@drawable/rating_bar_fill" /> <!--this is your .png file--> 
    <item 
     android:id="@android:id/progress" 
     android:drawable="@drawable/rating_bar_fill" /> <!--this is your .png file--> 
</layer-list> 

3-最後一件事是提供在.png文件的各種可繪製的文件夾。

11

在我的情況下,麻煩在於SVG drawables在圖層列表中。當我把它們改爲PNG它開始正常工作