How can I decrease the size of Ratingbar?

后端 未结 16 2178
Happy的楠姐
Happy的楠姐 2020-12-12 20:08

In my activity I have some Rating bars. But the size of this bar is so big! How can I make it smaller?

Edit

Thanks to Gabriel Negut, I did

相关标签:
16条回答
  • 2020-12-12 20:21

    If you are using Rating bar in Linearlayout with weightSum. Please make sure that you should not assign the layout_weight for rating bar. Instead, that place rating bar inside relative layout and give weight to the relative layout. Make rating bar width wrap content.

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_weight="30"
        android:layout_gravity="center"
        android:layout_height="wrap_content">
            <RatingBar
                android:id="@+id/userRating"
                style="@style/Widget.AppCompat.RatingBar.Small"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:stepSize="1" />
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-12 20:21

    In RatingBar give attribute:

    style="?android:attr/ratingBarStyleIndicator" 
    
    0 讨论(0)
  • 2020-12-12 20:22

    If you want to show the rating bar in small size, then just copy and paste this code in your project.

      <RatingBar
        android:id="@+id/MyRating"
        style="?android:attr/ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/getRating"
        android:isIndicator="true"
        android:numStars="5"
        android:stepSize="0.1" />
    
    0 讨论(0)
  • 2020-12-12 20:22

    In the RatingBar tag, add these two lines

    style="@style/Widget.AppCompat.RatingBar.Small"
    android:isIndicator="false"
    
    0 讨论(0)
  • 2020-12-12 20:22

    For those who created rating bar programmatically and want set small rating bar instead of default big rating bar

    
        private LinearLayout generateRatingView(float value){
            LinearLayout linearLayoutRating=new LinearLayout(getContext());
            linearLayoutRating.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
            linearLayoutRating.setGravity(Gravity.CENTER);
            RatingBar ratingBar = new RatingBar(getContext(),null, android.R.attr.ratingBarStyleSmall);
            ratingBar.setEnabled(false);
            ratingBar.setStepSize(Float.parseFloat("0.5"));//for enabling half star
            ratingBar.setNumStars(5);
            ratingBar.setRating(value);
            linearLayoutRating.addView(ratingBar);
            return linearLayoutRating;
        }
    
    
    0 讨论(0)
  • 2020-12-12 20:23

    The original link I posted is now broken (there's a good reason why posting links only is not the best way to go). You have to style the RatingBar with either ratingBarStyleSmall or a custom style inheriting from Widget.Material.RatingBar.Small (assuming you're using Material Design in your app).

    Option 1:

    <RatingBar
        android:id="@+id/ratingBar"
        style="?android:attr/ratingBarStyleSmall"
        ... />
    

    Option 2:

    // styles.xml
    <style name="customRatingBar"   
        parent="android:style/Widget.Material.RatingBar.Small">
        ... // Additional customizations
    </style>
    
    // layout.xml
    <RatingBar
        android:id="@+id/ratingBar"
        style="@style/customRatingBar"
        ... />
    
    0 讨论(0)
提交回复
热议问题