<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RatingBar
android:id="@+id/rating_bar"
style="@style/RadingStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="2.5" />
</LinearLayout>
<style name="RadingStyle" parent="@android:style/Widget.RatingBar">
<!-- 定义星星图片 -->
<item name="android:progressDrawable">@drawable/rating_bar</item>
<!-- 根据自定义星星图片的大小,设置相应的值,否则可能显示不全 -->
<item name="android:minHeight">20dp</item>
<item name="android:stepSize">0.5</item>
</style>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 必须使用此id,drawable为 未选中 状态的图片 -->
<item
android:id="@android:id/background"
android:drawable="@mipmap/cog_rad"/>
<!-- 必须使用此id,drawable为 半选中 状态的图片 -->
<item
android:id="@android:id/secondaryProgress"
android:drawable="@mipmap/cog_rad"/>
<!-- 必须使用此id,drawable为 全选中 状态的图片 -->
<item
android:id="@android:id/progress"
android:drawable="@mipmap/cog_blue"/>
</layer-list>
public class RatingBarActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitiy_rating_bar);
RatingBar ratingBar = findViewById(R.id.rating_bar);
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
Log.e("输出当钱显示的选择的条目", "==" + rating);
}
});
}
}
2019-08-04 11:58:14.355 14482-14482/liang.test.myapplication E/输出当前显示的选择的条目: ==0.5
2019-08-04 11:58:26.108 14482-14482/liang.test.myapplication E/输出当前显示的选择的条目: ==3.5
image.png image.png
网友评论