2016-08-03 58 views
10

我已將ratingBar顏色設置爲與Android的默認藍色和灰色不同的顏色 - 如果選中它們,我使用黑色背景和粉色製作了我的星星。Android RatingBar:無法填充部分ratingBar如果我定製它的顏色

這是我爲我的mainactivity代碼:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingbar); 
     LayerDrawable layerDrawable2 = (LayerDrawable) ratingBar.getProgressDrawable(); 
     DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(0)), 
       ContextCompat.getColor(getApplicationContext(), android.R.color.background_dark)); 
     DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(1)), 
       ContextCompat.getColor(getApplicationContext(), R.color.colorAccent)); // Partial star 
     DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(2)), 
       ContextCompat.getColor(getApplicationContext(), R.color.colorAccent)); 
     ratingBar.setIsIndicator(false); 
     ratingBar.setRating(3.6f); 
     ratingBar.setStepSize(0.1f); 
     ratingBar.invalidate(); 
     ratingBar.setIsIndicator(true); 
    } 
} 

你可以看到,如果我把在等級爲3.6,它選擇最多4

enter image description here

但是,如果我將我自己的自定義顏色註釋掉了,ratingBar自行設置正確,只顯示第四顆星的一部分,因爲傳遞給它的值爲3.6:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingbar); 
/*  LayerDrawable layerDrawable2 = (LayerDrawable) ratingBar.getProgressDrawable(); 
     DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(0)), 
       ContextCompat.getColor(getApplicationContext(), android.R.color.background_dark)); 
     DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(1)), 
       ContextCompat.getColor(getApplicationContext(), R.color.colorAccent)); // Partial star 
     DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(2)), 
       ContextCompat.getColor(getApplicationContext(), R.color.colorAccent));*/ 
     ratingBar.setIsIndicator(false); 
     ratingBar.setRating(3.6f); 
     ratingBar.setStepSize(0.1f); 
     ratingBar.invalidate(); 
     ratingBar.setIsIndicator(true); 
    } 
} 

enter image description here

爲什麼,如果我創建自己的自定義顏色的的RatingBar未正確填入自己?

我創建了一個小的回購,你可以下載它,並在這裏進行測試:https://github.com/Winghin2517/RatingBarTest2

編輯:

FlyingPumba圖書館顯示了什麼,我想實現一些希望,但是我需要一個風格,其中星星背景在屏幕上出現時爲白色,當用戶按下星號時,它將以純色填充。就我而言,我希望它充滿綠色。

以下是我所能達到的,但還沒有完成。我改變了滾動視圖的背景,其中的星星坐着粉紅色,以便你可以看到,而不是星星的白色背景,來自scrollview背景的粉紅色閃耀。對於我的使用案例,我希望白色星星坐在imageview前面,我不希望圖像視圖中的圖像通過星星閃耀。

另外我並不需要白色的中風,因爲我想保持星星的造型簡單。

<com.iarcuschin.simpleratingbar.SimpleRatingBar 
    android:id="@+id/ratingBar3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="8dp" 
    app:srb_rating="3.6" 
    app:srb_fillColor="@color/material_greenA700" 
    app:srb_borderColor="@color/material_white" 
    app:srb_starBorderWidth="0" 
    app:srb_pressedFillColor="@color/material_greenA700" 
    app:srb_starSize="30dp" 
    /> 

enter image description here

+0

我在庫中添加了一些功能,可以讓你完成你想要的功能(查看我添加到答案中的示例佈局)。 – FlyingPumba

回答

4

你在正確的跟蹤你正在嘗試的東西。你做錯了唯一的事情就是把錯誤的顏色分配給第二個drawable。

想象第二個可繪製爲背景的明星,而不是前景。所以部分星星需要背景顏色,而不是星星的顏色,例如黑色,而不是粉紅色。

以下是更正後的代碼,其工作方式如上所述。我已稍微重新格式化以突出所需的更改。

RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingbar); 
    LayerDrawable layerDrawable2 = (LayerDrawable) ratingBar.getProgressDrawable(); 

    // No star 
    DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(0)), 
      ContextCompat.getColor(getApplicationContext(), 
        android.R.color.background_dark)); 

    // Partial star 
    DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(1)), 
      ContextCompat.getColor(getApplicationContext(), 
        // use background_dark instead of colorAccent 
        // R.color.colorAccent)); 
        android.R.color.background_dark)); 

    // Custom star 
    DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(2)), 
      ContextCompat.getColor(getApplicationContext(), 
        R.color.colorAccent)); 

另一種方式在許多情況下,要做到這一點,並可能更好,是對RatingBar使用正確的造型。

這需要爲評級欄設置自定義樣式,然後定義該樣式使用的圖層可繪製以指向您自己的圖像或顏色。

我不打算進一步詳細了,因爲the actual xml code is shown in Anantham's answer

+2

你是對的。謝謝! – Simon

-1

你只需要.mutate()更新DrawableCompat.wrap(..)繪製。你的代碼是如下樣子:

DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(0).mutate()), 
      ContextCompat.getColor(getApplicationContext(), android.R.color.background_dark)); 
    DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(1).mutate()), 
      ContextCompat.getColor(getApplicationContext(), R.color.colorAccent)); // Partial star 
    DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(2).mutate()), 
      ContextCompat.getColor(getApplicationContext(), R.color.colorAccent)); 

如果顏色變爲dynammicaly提拉必須可變

見問題:Android: Tint using DrawableCompat

和答案: https://stackoverflow.com/a/30928051/2949612

您可以看到Drawable mutations博客由羅曼蓋伊和也見mutate()方法Android開發人員文檔。

我希望它能幫助你。

+0

請在發佈答案之前用我的回購測試此問題 - 我剛剛嘗試使用mutate()進行回購,但它不起作用。 – Simon

+1

repo更新爲mutate,因此您可以下載並自行測試。 – Simon

+0

好的。我將測試 – pRaNaY

-1

使用股票RatingBar可以很痛苦,你應該嘗試這個庫:SimpleRatingBar

(我有點偏頗,因爲我的創造者)

它的特點:

  • 完全工作android:layout_width:它可以被設置爲wrap_contentmatch_parent或abritary DP。
  • 任意數量的星星。
  • 任意步長。
  • 恆星的大小可以精確控制或設置最大尺寸。
  • 正常狀態下的可自定義顏色(邊界,填充和恆星背景)。
  • 按下狀態下可自定義的顏色(邊框,填充和星星背景)。
  • 星星之間的可自定義尺寸分離。
  • 可定製的星星的邊框寬度。
  • 可自定義的星星角落半徑。
  • 允許設置OnRatingBarChangeListener
  • 星星填充可以設置爲從左到右或從右到左(RTL語言支持)開始。
  • AnimationBuilder集成在視圖中,以編程方式用動畫設置評分。

Here is a preview of it

您可以在jcenterMaven Central中找到它。因此,在您build.gradle文件只是添加到您的依賴關係:

compile 'com.iarcuschin:simpleratingbar:0.1.+'

編輯: 我最近添加的改變恆星的背景色(空色)和禁用邊框的可能性。 達到你想要的,你可以添加到您的佈局是什麼:

<com.iarcuschin.simpleratingbar.SimpleRatingBar 
    android:id="@+id/ratingBar3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="8dp" 
    app:srb_rating="3.6" 
    app:srb_starBackgroundColor="@color/white" 
    app:srb_fillColor="@color/material_greenA700" 
    app:srb_borderColor="@color/material_white" 
    app:srb_pressedFillColor="@color/material_greenA700" 
    app:srb_starSize="30dp" 
    app:srb_drawBorderEnabled="false" 
    /> 

我希望它是有用的。

+0

非常令人印象深刻的庫,並且您將這一切都用視圖類從android擴展到ratingbar類。我對你的圖書館有疑問,我會將其發佈在我的問題中,你可以在那裏回答。 – Simon

1

在Activity.java:

的OnCreate:

 RatingBar setRatingBar = (RatingBar) findViewById(R.id.setRating); 
     float rating = Float.parseFloat(listString); 
     // onRatingChanged(setRatingBar, 0.0f, true); 

     public void onRatingChanged(RatingBar rateBar, float rating, boolean fromUser) { 
    try { 
     DecimalFormat decimalFormat = new DecimalFormat("#.#"); 
     curRate = Float.valueOf(decimalFormat.format((curRate * count + rating)/ ++count)); 
     float userRatePoint = curRate/**20*/; 

     setRatingBar.setRating(userRatePoint); 
     countText = (TextView) findViewById(R.id.countText); 
     countText.setText(userRatePoint + " Ratings"); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

然後嘗試一下本作smallstar的評價吧:

<RatingBar 
    android:id="@+id/setRating" 
    style="?android:attr/ratingBarStyleSmall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="10dp" 
    android:layout_marginTop="3dp" 
    android:isIndicator="true" 
    android:numStars="5" 
    android:stepSize="0.1" /> 
     //if you want to style please use "foodRatingBar" 
     //for custom star image use "ratingstar" 

style.xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<style name="foodRatingBar" parent="@android:style/Widget.RatingBar"> 
    <item name="android:progressDrawable">@drawable/ratingstar</item> 
    <item name="android:minHeight">22dip</item> 
    <item name="android:maxHeight">22dip</item> 
</style> 

值V11 /樣式。XML

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<style name="foodRatingBar" parent="@android:style/Widget.RatingBar"> 
    <item name="android:progressDrawable">@drawable/ratingstar</item> 
    <item name="android:minHeight">22dip</item> 
    <item name="android:maxHeight">22dip</item> 
</style> 

值V14/styles.xml

<resources> 
    <style name="foodRatingBar" parent="@android:style/Widget.RatingBar"> 
    <item name="android:progressDrawable">@drawable/ratingstar</item> 
    <item name="android:minHeight">22dip</item> 
    <item name="android:maxHeight">22dip</item> 
</style> 
<!-- 
    Base application theme for API 14+. This theme completely replaces 
    AppBaseTheme from BOTH res/values/styles.xml and 
    res/values-v11/styles.xml on API 14+ devices. 
--> 
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar"> 
    <!-- API 14 theme customizations can go here. --> 
</style> 
</resources> 

ratingstar.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/ratingstar_default" /> 
     <item android:id="@android:id/secondaryProgress" 
      android:drawable="@drawable/ratingstar_default" /> 
     <item android:id="@android:id/progress" 
      android:drawable="@drawable/ratingstar_selected" /> 
    </layer-list> 
</resources> 
+0

這看起來不像指標評級欄? – Simon

+0

addListenerOnRatingBar()在這個方法中,我實現了「setOnRatingBarChangeListener」我也實現了我的應用程序的評分欄 – YUVRAJ

+0

你可以告訴我什麼是你的風格/ foodratingbar嗎? – Simon