我想用anko创建cardView并设置cornerRadius参数。但是当我尝试做的时候 - 没有这样的不同之处。 在大班我这样做:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup): View {
with(applicationContext!!) {
listView = listView {
layoutParams = ViewGroup.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT)
dividerHeight = 20
}
}
listView?.adapter = CustomAdapter(forms)
return listView!!
}
在CustomAdapter中,我返回cardView,如下所示:
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val currentForm = getItem(position)
return convertView ?: createCardView(parent!!.context, currentForm)
}
private fun createCardView(context: Context, form: FormField): View =
with(context) {
frameLayout {
cardView {
layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT).apply {
leftMargin = dip(10)
rightMargin = dip(10)
topMargin = dip(5)
bottomMargin = dip(5)
}
backgroundColor = Color.WHITE
radius = dip(8).toFloat()
verticalLayout {
// title
textView {
text = form.title
textColor = ContextCompat.getColor(context, R.color.colorPrimary)
textSize = 20f
}.lparams(width = matchParent) {
leftMargin = dip(15)
topMargin = dip(10)
bottomMargin = dip(10)
}
// subtitle
textView {
if (form.subTitle != null) {
text = form.subTitle
textColor = ContextCompat.getColor(context, R.color.colorPrimary)
textSize = 12f
visibility = View.VISIBLE
} else {
visibility = View.GONE
}
}.lparams(width = matchParent) {
leftMargin = dip(15)
topMargin = dip(10)
bottomMargin = dip(10)
}
}.lparams(width = matchParent, height = matchParent)
}
}
}
我尝试以不同的方式和值调用'radius'setter,但结果总是这样
如您所见 - 角落总是矩形。我想要的 - 用Anko绕过角落
小p.s. - 当我从具有相同cardview的getView
充气xml布局返回时 - 它有圆角。
答案 0 :(得分:3)
所以,问题在于
backgroundColor = Color.WHITE
它将默认背景DRAWABLE参数设置为ColorDrawable而不是内部RoundRectDrawable
。
所以,当我将此行更改为:
background.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP)
所有开始工作,角落变得圆润
答案 1 :(得分:0)
先前的答案对我没有帮助。这对我有用:
cardView {
background = GradientDrawable().apply {
shape = GradientDrawable.RECTANGLE
cornerRadius = 8f
setStroke(2, grey)
....
}
}