The implementation of Ratingbar is actually very simple, just write it in the XML layout file.
Example:
In the main layout file, just write <RatingBar/>
Main.xml
1<RelativelayoutXmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4Android:layout_height= "Match_parent"5Tools:context= "Com.jorlee.ratingbar.MainActivity">67<Ratingbar8Android:id= "@+id/rating_bar"9Style= "@style/myratingbar"10Android:layout_height= "Wrap_content"11 Android:layout_width=" wrap_content "12 Android:numstars= "5" 13 Android:stepsize= " 0.1 "14 Android:rating = "2.5" >15 </< Span style= "color: #800000;" >ratingbar>16 17 Span style= "color: #008080;" >18 </relativelayout >
Ratingbar Layout_height and Layout_width are generally set to wrap_content, to avoid the problem of picture display is not complete
Numstars is the number of stars displayed, that is, the scoring series
Stepsize can be understood as a rating unit, set here to 0.1 is the smallest can be rated 0.1 stars
Rating is the number of stars currently scored, which is set to 2.5 is currently two and a half stars are rated
In the 9th line of the Main.xml file, the style is the key to customizing the Ratingbar, so you need to write an XML file in the Res/values directory
Ratingbar.xml
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Resources>3 <stylename= "Myratingbar"Parent= "@android: Style/widget.ratingbar">4<Itemname= "Android:progressdrawable">@drawable/rating_bar</Item>5<Itemname= "Android:minheight">44dip</Item>6<Itemname= "Android:maxheight">44dip</Item>7</style>8 9 </Resources>
where style name = "Myratingbar" Inside the name "Myratingbar" to correspond to the style in the Main.xml file= "@style/myratingbar"
<item name= "Android:minheight" >44dip</item>
<item name= "Android:maxheight">44dip</Item>
These two are set to the size of the Ratingbar
< Span style= "color: #0000ff;" >< Span style= "color: #0000ff;" ><item name= "android:progressdrawable" > @drawable/rating_bar</item>
This is set Ratingbar style, the default ratingbar is a picture of the stars, if you want to use their own pictures, just write a rating_bar.xml file in the Res/drawable directory
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <layer-listxmlns:android= "Http://schemas.android.com/apk/res/android">3 4 <ItemAndroid:id= "@+android:id/background"5 android:drawable= "@drawable/pre_unpre"></Item>6 7 <ItemAndroid:id= "@+android:id/secondaryprogress"8 android:drawable= "@drawable/empty">9 Ten </Item> One A <ItemAndroid:id= "@+android:id/progress" - android:drawable= "@drawable/all"> - </Item> the - </layer-list>
Secondaryprogress set to a picture when there is no rating
Progress set to post-rated slices
The system automatically generates a picture of the effect of the intermediate process
Background effects can also be set in background, where I wrote a click on the effect of Ratingbar change
Pre_unpre.xml
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <selectorxmlns:android= "Http://schemas.android.com/apk/res/android">3 4 <Itemandroid:state_pressed= "true"5 android:drawable= "@drawable/bg710">6 </Item>7 8 <Itemandroid:state_pressed= "false"9 android:drawable= "@drawable/empty"></Item>Ten </selector>
At this point, Ratingbar's custom is finished.
Paste on: (because the picture is casually looking for, and also looking for stars, very ugly, do not spray)
Customization of Ratingbar