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)
小getView
ps- 当我从具有相同cardview的膨胀xml布局返回时,它具有圆角。
因此,问题出在
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)
一切开始工作,角落变得圆润
归档时间: |
|
查看次数: |
1134 次 |
最近记录: |