首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Anko radius不工作

Anko radius不工作
EN

Stack Overflow用户
提问于 2018-01-17 16:05:53
回答 2查看 1.3K关注 0票数 3

我想用anko创建cardView并将cornerRadius参数设置为它。但是当我尝试这样做的时候--没有这样的不同。在main类中,我这样做:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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返回时,它有圆角。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-01-17 17:21:29

所以,问题出在

代码语言:javascript
复制
backgroundColor = Color.WHITE

将默认的背景可绘制参数设置为ColorDrawable,而不是内部RoundRectDrawable。因此,当我将此行更改为:

代码语言:javascript
复制
background.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP)

所有人都开始工作,圆角变得圆润起来

票数 6
EN

Stack Overflow用户

发布于 2019-05-27 18:46:09

之前的答案对我没有帮助。这对我来说很有效:

代码语言:javascript
复制
cardView {    
    background = GradientDrawable().apply {
        shape = GradientDrawable.RECTANGLE
        cornerRadius = 8f
        setStroke(2, grey)
        ....
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48296272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档