如何缩小RatingBar?

140

我在布局中添加了一个评分栏(RatingBar)

<RatingBar 
    android:id="@+id/ratingbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:stepSize="1.0"
    />  

但是默认的评分条样式太大了。
我尝试通过添加android样式来更改它:style="?android:attr/ratingBarStyleSmall"

但结果太小了,使用此属性无法设置评分。

我该怎么做?

19个回答

3
尽管Farry的答案可行,但对于三星设备,RatingBar会随机选择蓝色而不是我定义的颜色。因此,请使用:
style="?attr/ratingBarStyleSmall"

以下是如何使用它的完整代码:

<android.support.v7.widget.AppCompatRatingBar
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="?attr/ratingBarStyleSmall" // use smaller version of icons
                android:theme="@style/RatingBar"
                android:rating="0"
                tools:rating="5"/>

<style name="RatingBar" parent="Theme.AppCompat">
        <item name="colorControlNormal">@color/grey</item>
        <item name="colorControlActivated">@color/yellow</item>
        <item name="android:numStars">5</item>
        <item name="android:stepSize">1</item>
    </style>

2
小评级条的最佳解决方案
<RatingBar
                    android:layout_width="wrap_content"
                    style = "?android:attr/ratingBarStyleSmall"
                    android:layout_height="wrap_content" />

2

使用此代码

<RatingBar
    android:id="@+id/ratingBarSmalloverall"
    style="?android:attr/ratingBarStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="30dp"
    android:isIndicator="false"
    android:numStars="5" />

2
<RatingBar
    android:id="@+id/ratingBar1"
    android:numStars="5"
    android:stepSize=".5"
    android:rating="3.5"
    style="@android:style/Widget.DeviceDefault.RatingBar.Small"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

1
使用以下代码创建带有onTouch事件的小评分条:
enter code here


<RatingBar
            android:id="@+id/ratingBar"
            style="?android:ratingBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:isIndicator="false"
            android:rating="4"
            android:stepSize="0.5" />

0
经过大量研究,我发现减小自定义评分栏大小的最佳解决方案是按以下尺寸获取进度可绘制区域的大小:
xxhdpi - 48*48像素
xhdpi - 36*36像素
hdpi - 24*24像素
并在样式中将minheight和maxheight设置为16 dp,适用于所有分辨率。
如果这些尺寸对于您来说不是最佳的小型评分栏,请告知我。我认为这些尺寸与ratingbar.small样式属性相当兼容和等效。

0

对我来说有效,在评分栏的xml样式中添加“?android:attr/ratingBarStyleSmall”,它会起作用。


0
我认为在添加了这个style="?android:attr/ratingBarStyleSmall"之后,通过调整比例,它会起作用。
<RatingBar
            style="?android:attr/ratingBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:numStars="5"
            android:progressTint="#FFD700"
            android:rating="2.5"
            android:scaleX="0.50"
            android:scaleY="0.50"
            android:stepSize="0.5"
            android:transformPivotY="0dp" />

-4
我解决了这个问题,方法如下: style="?android:attr/ratingBarStyleSmall"

这就是原帖作者已经尝试过的,而其他答案也已经提到的内容。 - Pang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接