一、RatingBar几个常用属性
- numStars:星的总数量
- stepSize:变化的步长
- isIndicator:false:允许拖动改变评分条 true:不允许通过拖动改变评分条
- rating:初始的星数量
要想RatingBar显示设置的星总数,它的layout_width必须设置为wrap_content
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cn.ucai.day05_12_02_style.RatingbarActivity">
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:isIndicator="false"
android:rating="3"
android:stepSize="0.5"/>
</RelativeLayout>
星的总数为5,初次显示3颗星,效果图
RatingBar.png
RatingBar监听事件RatingBar.OnRatingBarChangeListener
RatingBar bar= (RatingBar)findViewById(R.id.ratingBar);
bar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener()
{
@Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
if(b){
Toast.makeText(RatingbarActivity.this,"拖动星星数量"+v,Toast.LENGTH_SHORT).show();
}
}});
二、自定义RatingBar几个常用属性
自定义RatingBar关键在于使用标签<layer-list/>以将多个图片按照顺序层叠起来。
分别设置固定id属性为@android:id/background和进度@android:id/progress
<?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/star_unselected"/>
<item
android:id="@android:id/progress"
android:drawable="@drawable/star_selected"/>
</layer-list>
网友评论