简体   繁体   English

Android 如何增加评级栏中星星的高度和宽度

[英]Android how to increase height and width of star in ratingbar

I am implementing ratingbar and I want to set ratingbar like below attached image but the problem is that when I use match_parent of ratingbar width then more than five star show in ratingbar .我采取ratingbar ,我想设置ratingbar像下面附加的图像,但问题是,当我使用match_parentratingbar宽度则超过五个明星秀ratingbar Can anyone help me how to implement my rating bar as attached image or suggest me how to implement it.任何人都可以帮助我如何将我的评级栏作为附加图像实施或建议我如何实施它。 Here is my activity_main这是我的activity_main

    <?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>

Here is my Main Activity.这是我的主要活动。

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

Here is my style这是我的风格

 <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>

在此处输入图片说明

If you are willing to use a RatingBar different to the Android's default, I'm the creator of: SimpleRatingBar .如果您愿意使用与 Android 的默认值不同的 RatingBar,那么我就是: SimpleRatingBar的创建者。

It features:它的特点是:

  • Fully working android:layout_width : it can be set to wrap_content , match_parent or abritary dp.完全可用的android:layout_width :它可以设置为wrap_contentmatch_parent或 abritary dp。
  • Arbitrary number of stars.任意数量的星星。
  • Arbitrary step size.任意步长。
  • Size of stars can be controlled exactly or by setting a maximum size.星星的大小可以精确控制或通过设置最大大小来控制。
  • Customizable colors in normal state (border, fill and background of stars and rating bar).正常状态下的可自定义颜色(星星和评级栏的边框、填充和背景)。
  • Customizable colors in pressed state (border, fill and background of stars and rating bar).按下状态下的可自定义颜色(边框、填充和星星和评级栏的背景)。
  • Customizable size separation between stars.可定制的星星之间的大小分隔。
  • Customizable border width of stars.可自定义的星星边框宽度。
  • Customizable stars corner radius.可定制的星星角半径。
  • Allows to set OnRatingBarChangeListener允许设置 OnRatingBarChangeListener
  • Stars fill can be set to start from left to right or from right to left (RTL language support).星星填充可以设置为从左到右或从右到左(RTL 语言支持)。
  • AnimationBuilder integrated in the view to set rating programatically with animation. AnimationBuilder 集成在视图中以通过动画以编程方式设置评级。

Here is a preview of it . 这是它的预览

You can find it either in jcenter or in Maven Central .您可以在jcenterMaven Central找到它。 So in your build.gradle file just add to your dependencies:因此,在您的build.gradle文件中,只需添加到您的依赖项中:

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

In your example, you can use it as:在您的示例中,您可以将其用作:

<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"
        />

No extra xml files needed :)不需要额外的 xml 文件:)

I hope it's useful.我希望它有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM