RatingBar控件是android自带的一个控件,在一些电商类的app上很常见,但是系统提供的控件图片实在太过单调,而美工又常常会设计一些稀奇古怪的图片用来展示ratingbar(笔者就曾经深受其害),那么应该怎样替换掉系统自带的这些图片呢?
其实说来也简单,就像把大象放入冰箱一样,只需要三步:
1. 在drawable下准备两张图片(没有找美工要),然后在drawable下准备一个图片资源ratingbar_drawable.xml
<?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/unselect">
</item>
<item
android:id="@+android:id/secondaryProgress"
android:drawable="@drawable/unselect">
</item>
<item
android:id="@+android:id/progress"
android:drawable="@drawable/selected">
</item>
</layer-list>
2.之后再styles.xml下添加一个标签,这个style继承父style Widget.RatingBar
<style name="roomRatingBar" parent="@android :style/Widget.RatingBar">
<item name="android:progressDrawable">@drawable/ratingbar_drawable</item>
<item name="android:minHeight">48dip</item>
<item name="android:maxHeight">48dip</item>
</style>
3.最后定义布局文件资源ratingbar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RatingBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/roomRatingBar"
android:layout_marginLeft="10dip"
android:id="@+id/ratingbar"
android:rating="1.5"
android:stepSize="0.15"
android:numStars="8"
/>
</LinearLayout>
自定义RatingBar就到此为止,运行后就能看到效果了。