2016-09-22 110 views
1

我實現ratingbar,我想設置ratingbar像下面附加的圖像,但問題是,當我使用ratingbarratingbar寬度match_parent那麼多的五星級表演。任何人都可以幫助我如何實現我的評級欄作爲附加圖像或建議我如何實現它。 這裏是我的activity_main的Android如何增加高度和星的寬度的RatingBar

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="gymtrainer.com.ratinbarexample.MainActivity"> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="20dp" 
     android:text="Rate 5 star for RatingBar" 
     /> 


    <RatingBar 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="20dp" 
     style="@style/foodRatingBar" 
     android:id="@+id/ratingbar_default" 
     android:maxWidth="250dp" 
     android:numStars="5" 
     android:stepSize="0.1" 
     /> 


    <TextView 
     android:id="@+id/textView" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="show state up checkbox" 
     android:textColor="#CC00CC" 
     android:textSize="20dp" 
     /> 


</LinearLayout> 

這是我的主要活動。

public class MainActivity extends Activity { 

    RatingBar ratingbar1; 
    Button button; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     addListenerOnButtonClick(); 

    } 

    public void addListenerOnButtonClick(){ 

     final TextView text = (TextView) findViewById(R.id.textView); 

     final RatingBar ratingBar_default = (RatingBar)findViewById(R.id.ratingbar_default); 

     final Button button = (Button)findViewById(R.id.button1); 
     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       ratingBar_default.setRating(5); 
       text.setText("Rating: "+String.valueOf(ratingBar_default.getRating())); 
      } 
     }); 

     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)); 
      }}); 
    } 
} 

這是我的風格

<style name="foodRatingBar" parent="@android:style/Widget.RatingBar"> 
    <item name="android:progressDrawable">@drawable/star_rating_bar_full</item> 
    <item name="android:minHeight">100dip</item> 
    <item name="android:maxHeight">100dip</item> 
</style> 

enter image description here

+0

Android不會縮放圖標。所以只需使用你的自定義圖標,並設置'minHeight'和'maxHeight'。 – Ironman

+1

我使用自定義庫編譯'com.iarcuschin:simpleratingbar:0.0.10' – amorenew

+0

@ amorenew謝謝你的幫助。沒有任何圖書館不可能做這件事情嗎? –

回答

1

如果您願意使用到Android的默認不同的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/ratingbar_default" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="20dp" 
     app:srb_numberOfStars="5" 
     app:srb_stepSize="0.1" 
     app:srb_starSize="100dp" 
     app:srb_borderColor="@color/your_light_green_color" 
     app:srb_fillColor="@color/your_light_green_color" 
     /> 

不需要額外的XML文件:)

希望這很有用。

相關問題