我想用anko创建cardView并将cornerRadius参数设置为它。但是当我尝试这样做的时候--没有这样的不同。在main类中,我这样做:
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在角落里转转

小附言-当我用同样的cardview从getView返回时,它有圆角。
发布于 2018-01-17 17:21:29
所以,问题出在
backgroundColor = Color.WHITE将默认的背景可绘制参数设置为ColorDrawable,而不是内部RoundRectDrawable。因此,当我将此行更改为:
background.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP)所有人都开始工作,圆角变得圆润起来
发布于 2019-05-27 18:46:09
之前的答案对我没有帮助。这对我来说很有效:
cardView {
background = GradientDrawable().apply {
shape = GradientDrawable.RECTANGLE
cornerRadius = 8f
setStroke(2, grey)
....
}
}https://stackoverflow.com/questions/48296272
复制相似问题