I don't see any method like set开发者_运维百科Style for class RatingBar. How can I set the rating style from code?
This works for me (source):
RatingBar rating =
new RatingBar(context, null, android.R.attr.ratingBarStyleSmall);
You need to set it in xml file! If you like to change it on the run, you can have two different View-s (rb) and switch them in the program (add/remove from layout) or use rb1.setVisibility(View.VISIBLE) and rb2.setVisibility(View.GONE);
<RatingBar
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_alignParentRight="true" android:id="@+id/rb1"
style="?android:attr/ratingBarStyleSmall"
android:clickable="false" android:numStars="5">
</RatingBar>
If you are programmatically creating the RatingBar as follows the
RatingBar rating = new RatingBar(context, null, android.R.attr.ratingBarStyleSmall);
then don't for get to add
rating.setIsIndicator(false);
else the touch will not work on RatingBar.
The android.R.attr.ratingBarStyleSmall
gives you very small stars if you want to have a slightly bigger stars use android.R.attr.ratingBarStyleIndicator
精彩评论