当前位置: 代码迷 >> Android >> Android更替RatingBar图片
  详细解决方案

Android更替RatingBar图片

热度:109   发布时间:2016-05-01 18:25:05.0
Android更换RatingBar图片

RatingBar是我们在系统显示分数的好组件,但是我们一般想把RatingBar装饰的更好看,想把更好看的图片用来替换系统默认的图片,系统默认的样子是这样的:

下图是个不错的选择哦:

想要实现如上效果,首先我们在styles.xml写入一个样式:

Xml代码 复制代码?收藏代码
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <resources>??
  3. ????<style?name="foodRatingBar"?parent="@android:style/Widget.RatingBar">??
  4. ????????<item?name="android:progressDrawable">@drawable/food_ratingbar_full</item>??
  5. ????????<item?name="android:minHeight">48dip</item>??
  6. ????????<item?name="android:maxHeight">48dip</item>??
  7. ????</style>??
  8. </resources>??
<?xml version="1.0" encoding="utf-8"?><resources>	<style name="foodRatingBar" parent="@android:style/Widget.RatingBar">		<item name="android:progressDrawable">@drawable/food_ratingbar_full</item>		<item name="android:minHeight">48dip</item>  		<item name="android:maxHeight">48dip</item>	</style></resources>


然后在Drawable文件夹下建food_rating_bar_full.xml文件,内容如下:

Xml代码 复制代码?收藏代码
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <layer-list?xmlns:android="http://schemas.android.com/apk/res/android">??
  3. ????<item?android:id="@+android:id/background"??
  4. ??????????android:drawable="@drawable/food_ratingbar_full_empty"?/>??
  5. ????<item?android:id="@+android:id/secondaryProgress"??
  6. ??????????android:drawable="@drawable/food_ratingbar_full_empty"?/>??
  7. ????<item?android:id="@+android:id/progress"??
  8. ??????????android:drawable="@drawable/food_ratingbar_full_filled"?/>??
  9. </layer-list>??
<?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/food_ratingbar_full_empty" />    <item android:id="@+android:id/secondaryProgress"          android:drawable="@drawable/food_ratingbar_full_empty" />    <item android:id="@+android:id/progress"          android:drawable="@drawable/food_ratingbar_full_filled" /></layer-list>


food_ratingbar_full_empty是代表没有选中图片效果,food_ratingbar_full_filled选中图片效果。
最后将style放入RatingBar中,即可实现你的图片效果:

Xml代码 复制代码?收藏代码
  1. <RatingBar???android:id="@+id/my_rating_bar"??
  2. ????????????... ??
  3. ????????????style="@style/foodRatingBar"?/>??
<RatingBar android:id="@+id/my_rating_bar"	...	style="@style/foodRatingBar" />



参考地址:http://kozyr.zydako.net/2010/05/23/pretty-ratingbar/

  相关解决方案