android中星级评分控件RatingBar的使用
16lz
2021-12-04
一.简单概述
1.相关属性:
> android:isIndicator:是否用作指示,用户无法更改,默认false> android:numStars:显示多少个星星,必须为整数 android:rating:默认评分值,必须为浮点数> android:stepSize: 评分每次增加的值,必须为浮点数> android:progressBackgroundTint="@color/goCart_btn" :未选中星星的颜色> android:progressTint="@color/goCart_btn" :选中星星的颜色> android:secondaryProgressTint="@color/goCart_btn" :选中星星的边框颜色
还有两种样式供我们选择
> style="?android:attr/ratingBarStyleIndicator"> style="?android:attr/ratingBarStyleSmall"
2.实现代码
<TextView android:id="@+id/item_tv" android:text="星级评分" android:textSize="16sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RatingBar android:rating="3.3" android:numStars="5" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RatingBar style="?android:attr/ratingBarStyleIndicator" android:rating="3.3" android:numStars="5" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RatingBar android:layout_marginTop="10dp" android:layout_marginLeft="5dp" style="?android:attr/ratingBarStyleSmall" android:rating="3.3" android:numStars="5" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
二.如何修改选中星星的颜色
<RatingBar android:rating="4.3" android:numStars="5" android:progressTint="#ffeca011" android:secondaryProgressTint="#ffeca011" android:progressBackgroundTint="#ffe7e7e7" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
三.示例展示
1.xml文件
这三行是一样的,所以就给出一行代码
<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"/> <RatingBar android:id="@+id/describe_score" android:layout_alignParentRight="true" style="@style/Widget.AppCompat.RatingBar.Indicator" android:isIndicator="false" android:progressTint="@color/goCart_btn" android:progressBackgroundTint="@color/eval_default_star" android:secondaryProgressTint="@color/goCart_btn" android:numStars="5" android:stepSize="1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout>
2.java代码
//监听值的变化 private void ratingBarListener() { describe_score.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { String score = String.valueOf(rating); scoreState1 = fun_getScoreState(score); describe_tip.setText(scoreState1); } }); }
//根据分数转换成文字展示 private String fun_getScoreState(String score) { String rating = ""; switch (score) { case "1.0": rating = "差"; break; case "2.0": rating = "较差"; break; case "3.0": rating = "一般"; break; case "4.0": rating = "好"; break; case "5.0": rating = "极好"; break; } return rating; }
更多相关文章
- android绘图看这篇就够了
- Android:res之selector背景选择器
- android中用Spannable在TextView中设置超链接、颜色、字体
- android中用Spannable在TextView中设置超链接、颜色、字体
- android中用Spannable在TextView中设置超链接、颜色、字体
- android list item divider line
- Android(安卓)控件相关
- Android设置窗口、控件透明度
- 项目中那些事|控件之TextView