简体   繁体   English

Android评分栏设置星级

[英]Android rating bar set star dimension

I am displaying rating bar in my application where i want to reduce the dimensions of the star. 我在我想缩小星星尺寸的应用程序中显示评分栏。 so here is xml of rating bar 所以这是评级栏的xml

       <RatingBar
                android:id="@+id/ratingBar"
                style="?android:attr/ratingBarStyleSmall"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="false"
                android:max="5"
                android:stepSize="0.25"
                android:numStars="0"
                android:focusable="false"
                android:layout_gravity="center_horizontal"
                android:focusableInTouchMode="false"
                android:isIndicator="true"/>

i am setting color and setting rating like this 我正在这样设置颜色和设置等级

 private RatingBar ratingBar;
 Drawable ratingBarColor = ratingBar.getProgressDrawable();
ratingBar = (RatingBar) view.findViewById(R.id.ratingBar);
 Drawable ratingBarColor = ratingBar.getProgressDrawable();
ratingBar.setRating((float) (value));
 DrawableCompat.setTint(ratingBarColor, ContextCompat.getColor(getActivity(), R.color.red));

But the problem is regardless of any value the color and decimal filling won't work.I am using style to reduce star dimensions.strange is if i remove style attribute in xml it will set exact value as star but dimension will reset to default.if i add min/max height to star the image will get cropped. 但是问题是无论任何值,颜色和十进制填充都不起作用。我正在使用样式来减小星形尺寸。奇怪的是,如果我在xml中删除样式属性,它将设置准确的值作为星形,但是尺寸将重置为默认值。如果我将最小/最大高度加到星形,图像将被裁剪。 here is the same problem :- How to set the custom size of the stars in RatingBar tried : style="?android:attr/ratingBarStyleIndicator" still same issue. 这是相同的问题:- 如何在RatingBar中设置星星的自定义大小,尝试过: style="?android:attr/ratingBarStyleIndicator"仍然是同一问题。 i am not getting what's wrong with the style attribute. 我没有得到样式属性有什么问题。

I don't know if it will be useful anymore, but I made a custom library which allows you to change star dimension programatically (among other stuff): SimpleRatingBar . 我不知道它是否会有用,但我创建了一个自定义库,该库可让您以编程方式(以及其他方法)更改星形尺寸: SimpleRatingBar

It features: 它具有以下特点:

  • Fully working android:layout_width : it can be set to wrap_content , match_parent or abritary dp. 可以正常使用android:layout_width :可以将其设置为wrap_contentmatch_parent或非婚生dp。
  • Arbitrary number of stars. 任意星数。
  • Arbitrary step size. 任意步长。
  • Size of stars can be controlled exactly or by setting a maximum size. 可以精确控制星星的大小,也可以设置最大大小。
  • Customizable colors in normal state (border, fill and background of stars and rating bar). 正常状态下的可自定义颜色(边框,星标的填充和背景以及等级栏)。
  • Customizable colors in pressed state (border, fill and background of stars and rating bar). 处于按下状态时的自定义颜色(边框,星号的填充和背景以及等级栏)。
  • Customizable size separation between stars. 可自定义星星之间的大小分隔。
  • Customizable border width of stars. 可自定义的星星边框宽度。
  • Customizable stars corner radius. 可自定义的星星角半径。
  • Allows to set OnRatingBarChangeListener 允许设置OnRatingBarChangeListener
  • Stars fill can be set to start from left to right or from right to left (RTL language support). 可以将星星填充设置为从左到右或从右到左开始(RTL语言支持)。
  • AnimationBuilder integrated in the view to set rating programatically with animation. 视图中集成了AnimationBuilder,可通过动画以编程方式设置评分。

Here is a preview of it . 这是它的预览

In your case, you would just have to do: 就您而言,您只需要执行以下操作:

ratingbar.setStarSize(20, Dimension.DP);

or, for example, in pixels: 或以像素为单位:

ratingbar.setStarSize(100, Dimension.PX);

You can find it either in jcenter or in Maven Central . 您可以在jcenterMaven Central找到它。 So in your build.gradle file just add to your dependencies: 因此,在您的build.gradle文件中,只需添加您的依赖项即可:

compile 'com.iarcuschin:simpleratingbar:0.1.+'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM