[英]Custom Rating Bar using Anko
我一直在探索使用Anko和Kotlin進行android開發,並且在等級欄上遇到了一些麻煩,即它的大小。 我嘗試使用自定義樣式將其縮小,但themedRatingBar似乎不起作用。 因此,我選擇制作自定義評分欄。 當我在主要活動中以這種方式設置它時,我似乎無法按照我想要的方式實現它:
starRatingView {
setRating(3)
}
它不會輸出等級3,而是會輸出默認等級0。
StarRatingView類:_LinearLayout {
lateinit var imageViewStars: List<ImageView> private var starNum: Float = 0f private var starSize: Int = 5 constructor(context: Context): super(context) { initializeView() } fun initializeView() { with(this) { linearLayout { relativeLayout { linearLayout { for (i in 1..starSize) imageView(R.drawable.ratingbar_empty) } linearLayout { for (i in 0..Math.round(starNum)) { imageView(R.drawable.ratingbar_filled) } } } } } } fun setSize(starSize: Int){ this.starSize = starSize } fun setRating(starNum: Float){ this.starNum = starNum }
}
上面是我用來創建自定義RatinBar的代碼。 盡量避免使用XML,而改用Anko。
如果可以幫助的話,可能的解決方案是使用Anko庫和Kotlin代碼在自定義提醒中顯示自定義RatingBar。
var rateGave: String? = null
alert {
title = "Rate your experience"
customView {
linearLayout {
ratingBar {
numStars = 5 //here is to define the number of stars you want
rating = 4f //starting rate to show as the alert pop up
setOnRatingBarChangeListener { ratingBar, rating, fromUser ->
rateGave = rating.toString()
}
}
}
}
positiveButton("Rate") { rate() }
}.show()
}
fun rate() {
println(rateGave) //now it's just printing out the rate to show that it's working fine, but in this function you can mange all the operations you need with the rating value
}
希望它會有用。 祝你今天愉快 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.