如何减小等级条的大小?

在我的活动中我有一些评分条。但是这个条的大小是如此之大! 我怎么才能把它变小呢?

剪辑

多亏了加布里埃尔 · 内格特, 我用下面的方式做到了:

<RatingBar
style = "?android:attr/ratingBarStyleSmall"
android:numStars = "5"
android:rating   = "4" />

现在,大小减少,但数量的星星和评级不起作用! ! ! 为什么? 我有7颗星,其中6颗被选中了。

169501 次浏览

我发布的原始链接现在已经坏了(有一个很好的理由来解释为什么只发布链接不是最好的方式)。你必须使用 ratingBarStyleSmall或者继承自 Widget.Material.RatingBar.Small的自定义样式(假设你在你的应用程序中使用的材质设计)来设计 RatingBar

选择1:

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

选择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"
... />

如果你只需要默认的样式,确保你有以下的宽度/高度,否则 numStars 可能会变得乱七八糟:

android:layout_width="wrap_content"
android:layout_height="wrap_content"

您可以在 RatingBar的 XML 代码中设置它,使用 scaleXscaleY进行相应的调整。“1.0”是正常的大小,“ .0”中的任何内容都会减小它,任何大于“1.0”的内容都会增加它。

<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="0.5"
android:scaleY="0.5" />

如果希望以较小的尺寸显示评级条,只需将此代码复制并粘贴到项目中即可。

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

这是我的解决办法后,很多斗争,以减少小尺寸的评级条,甚至没有丑陋的填充

 <RatingBar
android:id="@+id/listitemrating"
style="@android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX=".5"
android:scaleY=".5"
android:transformPivotX="0dp"
android:transformPivotY="0dp"
android:isIndicator="true"
android:max="5" />

RatingBar中给出属性:

style="?android:attr/ratingBarStyleIndicator"
<RatingBar
style="?android:attr/ratingBarStyleIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

工作示例

             <RatingBar
style="@style/RatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="3.5"
android:stepSize="0.5" />

并将其添加到您的样式 xml 文件中

<style name="RatingBar"
parent="android:style/Widget.Material.RatingBar.Small">
<item name="colorControlNormal">@color/primary_light</item>
<item name="colorControlActivated">@color/primary_dark</item>
</style>

这样你就不需要自定义评分条了。

下面的代码片段可以帮助我调整评分栏的大小

<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ratingBar"
style="?android:attr/ratingBarStyleIndicator"
android:scaleX=".5"
android:rating="3.5"
android:scaleY=".5"
android:transformPivotX="0dp"
android:transformPivotY="0dp"
android:max="5"/>

enter image description here

检查我的答案 给你。最好的方式实现它。

 <style name="MyRatingBar" parent="@android:style/Widget.RatingBar">
<item name="android:minHeight">15dp</item>
<item name="android:maxHeight">15dp</item>
<item name="colorControlNormal">@color/white</item>
<item name="colorControlActivated">@color/home_add</item>
</style>

然后像这样使用

<RatingBar
style="?android:attr/ratingBarStyleIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="false"
android:max="5"
android:rating="3"
android:scaleX=".8"
android:scaleY=".8"
android:theme="@style/MyRatingBar" />

如果你在 Linearlayout 使用等级条与重量和。请确保您不应该为等级条分配布局 _ 权重。相反,将评分条放在相对布局中,并赋予相对布局权重。制作额定条宽包装内容。

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

使用 Widget.AppCompat.RatingBar,您可以使用两种样式; IndicatorSmall分别适用于大小尺寸。

<RatingBar
android:id="@+id/rating_star_value"
style="@style/Widget.AppCompat.RatingBar.Small"
... />

对于那些以编程方式创建评级条并希望设置小评级条而不是默认的大评级条的用户



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


RatingBar标记中,添加以下两行

style="@style/Widget.AppCompat.RatingBar.Small"
android:isIndicator="false"
 <RatingBar     android:id="@+id/id_tv_rating_bar"
style="@style/Widget.AppCompat.RatingBar.Small"
android:layout_width="wrap_content"
android:layout_height="@dimen/_20sdp"
android:layout_marginLeft="@dimen/_80sdp"
android:numStars="5"
android:paddingTop="@dimen/_5sdp"
android:rating="5" />

只要用 style="@style/Widget.AppCompat.RatingBar.Small"就可以了,当然!

这招对我很管用。
Check this image

            android:id="@+id/ratingBar"
style="?android:attr/ratingBarStyleIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:scaleX=".8"
android:scaleY=".8"
android:stepSize="0.5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.543"
app:layout_constraintStart_toEndOf="@+id/title"
app:layout_constraintTop_toTopOf="parent" />

一定会成功的

style="?android:attr/ratingBarStyleSmall"