Anko CardView 半径不起作用
Anko CardView radius not work
我想用 anko 创建 cardView 并为其设置 cornerRadius 参数。但是当我尝试做时 - 没有这样的不同。
在主要 class 我这样做:
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 中我 return 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,但结果总是这样
如您所见 - 角始终为矩形。我想要的——和红豆一起转弯
小p.s。 - 当我 return 从 getView
膨胀 xml 布局与相同的 cardview - 它有圆角。
所以,问题出在
backgroundColor = Color.WHITE
已将默认背景 DRAWABLE 参数设置为 ColorDrawable 而不是内部 RoundRectDrawable
。
所以,当我将此行更改为:
background.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP)
全部开始工作,边角变圆
之前的回答对我没有帮助。这对我有用:
cardView {
background = GradientDrawable().apply {
shape = GradientDrawable.RECTANGLE
cornerRadius = 8f
setStroke(2, grey)
....
}
}
我想用 anko 创建 cardView 并为其设置 cornerRadius 参数。但是当我尝试做时 - 没有这样的不同。 在主要 class 我这样做:
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 中我 return 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,但结果总是这样
如您所见 - 角始终为矩形。我想要的——和红豆一起转弯
小p.s。 - 当我 return 从 getView
膨胀 xml 布局与相同的 cardview - 它有圆角。
所以,问题出在
backgroundColor = Color.WHITE
已将默认背景 DRAWABLE 参数设置为 ColorDrawable 而不是内部 RoundRectDrawable
。
所以,当我将此行更改为:
background.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP)
全部开始工作,边角变圆
之前的回答对我没有帮助。这对我有用:
cardView {
background = GradientDrawable().apply {
shape = GradientDrawable.RECTANGLE
cornerRadius = 8f
setStroke(2, grey)
....
}
}