一、属性
- android:isIndicator=“false” 此评级栏是否为指示器(用户不可更改)。 默认false 可更改,true 不可更改
- android:numStars=“6” 设置星星数量
- android:rating=“1” 设置默认评级
- android:stepSize=“2” 设置步长
二、示例
class MainActivity : AppCompatActivity() {
private lateinit var ratingBar: RatingBar
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initView()
initClick()
}
private fun initView() {
ratingBar = findViewById(R.id.ratingBar)
}
private fun initClick() {
ratingBar.setOnRatingBarChangeListener { ratingBar, rating, fromUser ->
Toast.makeText(this, "评级:$rating", Toast.LENGTH_SHORT).show()
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<RatingBar
android:id="@+id/ratingBar"
style="?android:attr/ratingBarStyleIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="false"
android:numStars="6"
android:rating="1"
android:stepSize="2" />
</LinearLayout>