Android中星星评分控件SimpleRatingBar的使用

android自带的RatingBar控件在设置大小上是很难控制的【RatingBar控件的介绍】 下面的话就来说说如何使用SimpleRatingBar开源库去实现星级效果【GitHub查看】

一.SimpleRatingBar的属性和方法
描述属性/方法
设置显示总星数app:srb_numberOfStars/setNumberOfStars(int)
设置选中星数app:srb_rating / 设置评级setRating(float)
设置步长app:srb_stepSize/ setStepSize(float)
设置星星大小app:srb_starSize/ setStarSize(float)
设置最大星号app:srb_maxStarSize/ setMaxStarSize(float)
设置边框宽度app:srb_starsSeparation/ setStarsSeparation(float)
设置边框宽度app:srb_starBorderWidth/ setStarBorderWidth(float)
设置星角半径app:srb_starCornerRadius/ setStarCornerRadius(float)
设置正常状态下的星形边框颜色app:srb_borderColor/ setBorderColor(@ColorInt int)
用正常状态设置星星填充颜色用app:srb_fillColor/ setFillColor(@ColorInt int)
用正常状态设置星星背景颜色app:srb_starBackgroundColor/setStarBackgroundColor(@ColorInt int)
设置正常状态下额定条的背景颜色app:srb_backgroundColor/ setBackgroundColor(@ColorInt int)
在按下状态设置星级边框颜色app:srb_pressedBorderColor/setPressedBorderColor(@ColorInt int)
设置星形填充颜色处于按下状态app:srb_pressedFillColor/setPressedFillColor(@ColorInt int)
在压制状态下设置星星背景颜色app:srb_pressedStarBackgroundColor/setPressedStarBackgroundColor(@ColorInt int)
用压力状态设置额定条的背景颜色app:srb_pressedBackgroundColor/setPressedBackgroundColor(@ColorInt int)
启用/禁用用户的交互app:srb_isIndicator/setIsIndicator(boolean)
启用/禁用星形边框app:srb_drawBorderEnabled/ setDrawBorderEnabled(boolean)
设置填充方向(左或右)app:srb_gravity/ setGravity(Gravity)
得到已选中星星的个数getRating()
得到一共有多少颗星星getNumberOfStars()
二.使用步骤
1.添加依赖
implementation 'com.iarcuschin:simpleratingbar:0.1.5'
2.示例
          <RelativeLayout
                android:id="@+id/star_describe"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/score_title1"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:text="描述相符"
                    android:textSize="13sp"
                    android:textColor="@color/theme_defaultText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <TextView
                    android:id="@+id/describe_tip"
                    android:textSize="12sp"
                    android:textColor="@color/theme_textColor"
                    android:layout_marginLeft="5dp"
                    android:layout_toRightOf="@+id/score_title1"
                    android:layout_centerVertical="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <com.iarcuschin.simpleratingbar.SimpleRatingBar
                    android:id="@+id/describe_score"
                    android:layout_alignParentRight="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:srb_starSize="28dp"  //星星大小
                    app:srb_numberOfStars="5"  //显示星星个数
                    app:srb_stepSize="1"    //步长
                    app:srb_drawBorderEnabled="false"
                    app:srb_starBackgroundColor="@color/disabled_textColor"
                    app:srb_fillColor="@color/goCart_btn" />
            </RelativeLayout>

SimpleRatingBar自带点击星星变亮和滑动星星变亮的事件,不需要我们另外写代码,如果要监听选中星星个数的改变,可以添加setOnRatingBarChangeListener监听,如下:

   SimpleRatingBar describe_score = findViewById(R.id.describe_score);
   describe_score.setOnRatingBarChangeListener(new SimpleRatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(SimpleRatingBar simpleRatingBar, float rating, boolean fromUser) {
             //这里是防止星星的个数变成零个,最少为一个
                if(rating==0){
                    describe_score.setRating(1);
                    rating=1;
                }
                score1 = String.valueOf(rating);
                scoreState1 = fun_getScoreState(score1);
                describe_tip.setText(scoreState1);
            }
        });
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值