gpt4 book ai didi

android - Anko CardView 半径不起作用

转载 作者:太空狗 更新时间:2023-10-29 16:11:10 26 4
gpt4 key购买 nike

我想用 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,但结果总是这样 enter image description here

如您所见 - 角始终为矩形。我想要的——和红 bean 一起转弯 enter image description here

小P.S. - 当我从 getView 返回时,使用相同的 cardview 膨胀 xml 布局 - 它有圆角。

最佳答案

所以,问题出在

backgroundColor = Color.WHITE

默认背景 DRAWABLE 参数设置为 ColorDrawable 而不是内部 RoundRectDrawable。所以,当我将此行更改为:

background.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP)

全部开始工作,边角变圆

关于android - Anko CardView 半径不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48296272/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com