How to set the custom size of the stars in RatingBar

。_饼干妹妹 提交于 2019-12-31 08:57:07

问题


I have already added a style

<style name="myRatingBar" parent="@android:style/Widget.RatingBar">
    <item name="android:minHeight">32dp</item>
    <item name="android:maxHeight">32dp</item>
</style>

However instead of scaled stars I'm receiving cropped stars:

Is there a way to change the size of the stars?


回答1:


Another approach would be scaling the widget:

android:scaleX="0.5"
android:scaleY="0.5"



回答2:


Android won't scale a rating bar icons. When you decrease the minHeight and maxHeight android will always crop the icons as shown on the picture. The workaround for it and seems to be the only one solution is to provide your own icons for stars with the desired dimensions and set the minHeight and maxHeight to the size of the icons.




回答3:


//you can also try

<style name="myRatingBar" parent="@android:style/Widget.RatingBar.Indicator">

<style name="myRatingBar" parent="@android:style/Widget.RatingBar.Small">

Ref: /android-sdk/platforms/android-10/data/res/values/styles.xml

<style name="Widget.RatingBar">
        <item name="android:indeterminateOnly">false</item>
        <item name="android:progressDrawable">@android:drawable/ratingbar_full</item>
        <item name="android:indeterminateDrawable">@android:drawable/ratingbar_full</item>
        <item name="android:minHeight">57dip</item>
        <item name="android:maxHeight">57dip</item>
        <item name="android:thumb">@null</item>
    </style>

    <style name="Widget.RatingBar.Indicator">
        <item name="android:indeterminateOnly">false</item>
        <item name="android:progressDrawable">@android:drawable/ratingbar</item>
        <item name="android:indeterminateDrawable">@android:drawable/ratingbar</item>
        <item name="android:minHeight">38dip</item>
        <item name="android:maxHeight">38dip</item>
        <item name="android:thumb">@null</item>
        <item name="android:isIndicator">true</item>
    </style>

    <style name="Widget.RatingBar.Small">
        <item name="android:indeterminateOnly">false</item>
        <item name="android:progressDrawable">@android:drawable/ratingbar_small</item>
        <item name="android:indeterminateDrawable">@android:drawable/ratingbar_small</item>
        <item name="android:minHeight">14dip</item>
        <item name="android:maxHeight">14dip</item>
        <item name="android:thumb">@null</item>
        <item name="android:isIndicator">true</item>
    </style>



回答4:


You solved your problem. But this may help others.

I have same issue.All solutions I got does not work for multiple screen sizes. After spending hours I ended with following solution.

Just get drawable height which you want to use at runtime and set it to RatingBar. Here is sample code.

Drawable starDrawable = getResources().getDrawable(R.drawable.YOUR_IMAGE);
int height = starDrawable.getMinimumHeight();
LayoutParams params = (LayoutParams) ratingBar.getLayoutParams();
params.height = height;
ratingBar.setLayoutParams(params);



回答5:


Look at this Custom Rating Bar in Small Style .

I think you have to use the Custom Style for Rating Bar . Here is the Example.




回答6:


This is not exactly an answer but I would like to show an alternative. Using a library called Iconify that allow you to use scalable vector icons on android apps and with a simple method you can create awesome ratingbar with icons.

The method

public static String montarEstrellasValoracion(float valoration,int starSize) {
    String result="";
    float cont=1f;

    for(float i=1f;i<=valoration;i++){
        if(valoracion==0){
            break;
        }
        cont=i;
        if((cont+0.5f)>=(valoration) && valoracion!=5f){
            result+="{fa-star "+starSize+"dp} ";
            result+="{fa-star-half-o "+starSize+"dp} ";
            cont++;
            break;
        }else if((cont+0.5f)<(valoration) && (cont+1f)>(valoration) && valoration!=5f){
            result+="{fa-star "+starSize+"dp} ";
            result+="{fa-star "+starSize+"dp} ";
            cont++;
            break;
        }

        if(cont<=5){
            result+="{fa-star "+starSize+"dp} ";
        }

    }

    for(float j=cont;j<5f;j++){
        result+="{fa-star-o "+starSize+"dp} ";
    }

    if(valoration==0){
        result+="{fa-star-o "+starSize+"dp}";
    }

    return result;
}

And then on the onCreate method:

IconTextView valoracion = (IconTextView)item.findViewById(R.id.id_icon_textview);

valoracion.setText(montarEstrellasValoracion(valoration,15)); valoracion.setTextColor(Color.parseColor("#000");

The icons are from another awesome site called Font Awesome

Hope this help anybody!




回答7:


             <RatingBar
                android:id="@+id/AVL_rating"
                style="?android:attr/ratingBarStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/ASD_cardlayout"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:layout_marginTop="@dimen/_10sdp"
                android:isIndicator="true"
                android:numStars="5"
                android:rating="5.0"
                android:secondaryProgressTint="@color/clr_red"
                android:stepSize="1.0" />



回答8:


Maybe we can use the another alternative that android provides small size of rating stars. Adding style="@style/Widget.AppCompat.RatingBar.Small" to the RatingBar.

            <RatingBar
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/list_item_margin"
                android:layout_marginLeft="@dimen/list_item_margin"
                android:numStars="5"
                android:isIndicator="true"
                style="@style/Widget.AppCompat.RatingBar.Small"/>



回答9:


just add font-size ppt to in style : style='font-size: 1.7vw'



来源:https://stackoverflow.com/questions/12382632/how-to-set-the-custom-size-of-the-stars-in-ratingbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!