Anko CardView半径不起作用

ale*_*spb 3 android kotlin anko cardview

我想用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!!
}
Run Code Online (Sandbox Code Playgroud)

在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)
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

我尝试用不同的方式和值来称呼“半径”二传手,但结果总是这样 在此处输入图片说明

如您所见-角始终为矩形。我想要的-与Anko一起转过弯 在此处输入图片说明

getViewps- 当我从具有相同cardview的膨胀xml布局返回时,它具有圆角。

ale*_*spb 6

因此,问题出在

backgroundColor = Color.WHITE
Run Code Online (Sandbox Code Playgroud)

它已将默认背景DRAWABLE参数设置为ColorDrawable,而不是inner RoundRectDrawable。因此,当我将此行更改为:

background.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP)
Run Code Online (Sandbox Code Playgroud)

一切开始工作,角落变得圆润