How to make a smaller RatingBar?
I've added a RatingBar in a layout:
<RatingBar
android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0"
/>
But the default style for the rating bar is too large.
I've try to change it by adding the android style :style="?android:attr/ratingBarStyleSmall"
But the result is too small and it's impossible 开发者_开发技巧to set a rate with this property.
How could I do?
How to glue the code given here ...
Step-1. You need your own rating stars in res/drawable
...
Step-2 In res/drawable
you need ratingstars.xml
as follow ...
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background"
android:drawable="@drawable/star_empty" />
<item android:id="@android:id/secondaryProgress"
android:drawable="@drawable/star_empty" />
<item android:id="@android:id/progress"
android:drawable="@drawable/star" />
</layer-list>
Step-3 In res/values
you need styles.xml
as follow ...
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="foodRatingBar" parent="@android:style/Widget.RatingBar">
<item name="android:progressDrawable">@drawable/ratingstars</item>
<item name="android:minHeight">22dip</item>
<item name="android:maxHeight">22dip</item>
</style>
</resources>
Step-4 In your layout ...
<RatingBar
android:id="@+id/rtbProductRating"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:numStars="5"
android:rating="3.5"
android:isIndicator="false"
style="@style/foodRatingBar"
/>
The default RatingBar widget is sorta' lame.
The source makes reference to style "?android:attr/ratingBarStyleIndicator
" in addition to the "?android:attr/ratingBarStyleSmall
" that you're already familiar with. ratingBarStyleIndicator
is slightly smaller but it's still pretty ugly and the comments note that these styles "don't support interaction".
You're probably better-off rolling your own. There's a decent-looking guide at http://kozyr.zydako.net/2010/05/23/pretty-ratingbar/ showing how to do this. (I haven't done it myself yet, but will be attempting in a day or so.)
p.s. Sorry, was going to post a link to the source for you to poke around in but I'm a new user and can't post more than 1 URL. If you dig your way through the source tree, it's located at frameworks/base/core/java/android/widget/RatingBar.java
Just use:
android:scaleX="0.5"
android:scaleY="0.5"
Change the scale factor according to your need.
In order to scale without creating a padding on the left also add
android:transformPivotX="0dp"
There is no need to add a listener to the ?android:attr/ratingBarStyleSmall
. Just add
android:isIndicator=false
and it will capture click events, e.g.
<RatingBar
android:id="@+id/myRatingBar"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:isIndicator="false" />
the small one implement by the OS
<RatingBar
android:id="@+id/ratingBar"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
apply this.
<RatingBar android:id="@+id/indicator_ratingbar"
style="?android:attr/ratingBarStyleIndicator"
android:layout_marginLeft="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
I found an easier solution than I think given by the ones above and easier than rolling your own. I simply created a small rating bar, then added an onTouchListener to it. From there I compute the width of the click and determine the number of stars from that. Having used this several times, the only quirk I've found is that drawing of a small rating bar doesn't always turn out right in a table unless I enclose it in a LinearLayout (looks right in the editor, but not the device). Anyway, in my layout:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RatingBar
android:id="@+id/myRatingBar"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5" />
</LinearLayout>
and within my activity:
final RatingBar minimumRating = (RatingBar)findViewById(R.id.myRatingBar);
minimumRating.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View view, MotionEvent event)
{
float touchPositionX = event.getX();
float width = minimumRating.getWidth();
float starsf = (touchPositionX / width) * 5.0f;
int stars = (int)starsf + 1;
minimumRating.setRating(stars);
return true;
}
});
I hope this helps someone else. Definitely easier than drawing one's own (although I've found that also works, but I just wanted an easy use of stars).
<RatingBar
android:rating="3.5"
android:stepSize="0.5"
android:numStars="5"
style = "?android:attr/ratingBarStyleSmall"
android:theme="@style/RatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
// if you want to style
<style name="RatingBar" parent="Theme.AppCompat">
<item name="colorControlNormal">@color/colorPrimary</item>
<item name="colorControlActivated">@color/colorAccent</item>
</style>
// add these line for small rating bar
style = "?android:attr/ratingBarStyleSmall"
<RatingBar
android:id="@+id/rating_bar"
style="@style/Widget.AppCompat.RatingBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
although answer of Farry works, for Samsung devices RatingBar took random blue color instead of the defined by me. So use
style="?attr/ratingBarStyleSmall"
instead.
Full code how to use it:
<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>
The best answer I got
<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>
user like this
<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" />
Increase/Decrease sizes by using scaleX and scaleY value
Use This Code
<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" />
<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" />
The Best Answer for small ratingbar
<RatingBar
android:layout_width="wrap_content"
style = "?android:attr/ratingBarStyleSmall"
android:layout_height="wrap_content" />
use the following code for small rating bar with onTouch event
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" />
After a lot of research I found the best solution to reduce the size of custom rating bars is to get the size of progress drawable in certain sizes as mentioned below :
xxhdpi - 48*48 px
xhdpi - 36*36 px
hdpi - 24*24 px
And in style put the minheight and maxheight as 16 dp for all Resolution.
Let me know if this doesn't help you to get a best small size rating bars as these sizes I found very compatible and equivalent to ratingbar.small style attribute.
Its working for me in rating bar xml style="?android:attr/ratingBarStyleSmall" add this.it will work.
I solve this issue the following: style="?android:attr/ratingBarStyleSmall"
精彩评论