自定义Ratingbar(五星评价)

效果如下图:



使用:

1)引用库:

compile 'com.xingliuhua:xlhratingbar_lib:1.0.1'

2)在drawable文件夹下自定义星星选中与未选中的状态:

<item android:drawable="@drawable/ic_report_lightstart"
      android:state_checked="true"></item>
<item android:drawable="@drawable/ic_report_darkstart"
      android:state_checked="false"></item>

3)在XML布局文件中使用:

<com.example.xlhratingbar_lib.XLHRatingBar
    xmlns:xlhRatingBar="http://schemas.android.com/apk/res-auto"
    android:id="@+id/ratingBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xlhRatingBar:canEdit="true"
    xlhRatingBar:differentSize="false"
    xlhRatingBar:dividerWidth="7dp"
    xlhRatingBar:countSelected="1"
    xlhRatingBar:starCount="5"
    xlhRatingBar:stateResId="@drawable/selector_star_appraise"
    xlhRatingBar:widthAndHeight="13dp" />

  • 属性解释:

    • canEdit: true 可以点击选择不同的星星,默认false
    • differentSize :  true 星星的大小可以不同(中间的最大,两端的依次变小且星星总数为奇数时才有效)
    • dividerWidth :单个星星之间的间隔
    • starCount :星星的总个数
    • widthAndHeight :星星的大小
    • countSelected :已经选择的星星的个数
    • stateResId:单个星星的不同状态(Android默认的drawable是comment_ratingbar_selector

4)在代码中使用:
XLHRatingBar ratingBar = (XLHRatingBar) findViewById(R.id.ratingBar);
       ratingBar.setCountNum(5);
       ratingBar.setCountSelected(1);
       ratingBar.setOnRatingChangeListener(new XLHRatingBar.OnRatingChangeListener() {
           @Override
           public void onChange(int countSelected) {
               tvResult.setText(countSelected + "");
           }
       });

5)与Android系统RatingBar对比:
1.更方便指定每个星星的图标、大小及各个星星的间距
2.支持监听选中状态的变化

6)原文:https://github.com/xingliuhua/XLHRatingBar

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值