Android 如何在 RatingBar 中设置星星的自定义大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12382632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:30:41  来源:igfitidea点击:

How to set the custom size of the stars in RatingBar

androidandroid-widget

提问by Marcin S.

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:

然而,我收到的不是缩放的星星,而是裁剪的星星:

enter image description here

在此处输入图片说明

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

有没有办法改变星星的大小?

采纳答案by Marcin S.

Android won't scale a rating bar icons. When you decrease the minHeightand maxHeightandroid 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 minHeightand maxHeightto the size of the icons.

Android 不会缩放评级栏图标。当您减少时minHeightmaxHeightandroid 将始终裁剪图标,如图所示。它的解决方法,似乎是唯一的解决方案是提供自己的图标恒星所需的尺寸和设置minHeight,并maxHeight以图标的大小。

回答by xleon

Another approach would be scaling the widget:

另一种方法是缩放小部件:

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

回答by Ketan Ahir

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.

只需获取要在运行时使用的可绘制高度并将其设置为 RatingBar。这是示例代码。

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

回答by Padma Kumar

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

参考:/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>

回答by Chirag

Look at thisCustom Rating Bar in Small Style .

看看这个小风格的自定义评级栏。

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

我认为您必须使用 Custom Style for Rating Bar 。是示例。

回答by Hictus

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

这不完全是一个答案,但我想展示一个替代方案。使用一个名为Iconify的库,它允许您在 android 应用程序上使用可缩放的矢量图标,并且通过一种简单的方法,您可以创建带有图标的很棒的评级栏。

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:

然后在 onCreate 方法上:

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

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

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

The icons are from another awesome site called Font Awesome

这些图标来自另一个很棒的网站Font Awesome

Hope this help anybody!

希望这可以帮助任何人!

回答by Mohan Kumar

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

回答by Kerisnarendra

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.

也许我们可以使用另一个替代方案,即 android 提供小尺寸的评级星级。将 style="@style/Widget.AppCompat.RatingBar.Small" 添加到 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"/>

回答by Rohan

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

只需font-size在样式中添加ppt 即可:style='font-size: 1.7vw'