ANKO paddingTop-无法重新分配Val

时间:2019-02-25 09:27:11

标签: android kotlin anko

我想将paddingTop放在我的verticalLayout上,但是会提示警告 无法重新分配Val 。我认为我以错误的方式解决了这个问题,但是在线资源似乎很匮乏。

下面是我的AnkoComponent:

class MainActivityUi : AnkoComponent<MainActivity> {
override fun createView(ui: AnkoContext<MainActivity>) = with(ui) {
    verticalLayout {
        textView {
            text = "Input"
            textColor = Color.BLACK
            textAlignment = View.TEXT_ALIGNMENT_CENTER
        }
        textView {
            text = "Output"
            textColor = Color.BLACK
            textAlignment = View.TEXT_ALIGNMENT_CENTER
        }
        linearLayout {
            button {

                text = "0"
            }.lparams(width = wrapContent, height = matchParent) {
                gravity = Gravity.CENTER
                weight = 5f
                margin = dip(3)
            }
            button {
                text = "1"
            }.lparams(width = wrapContent, height = matchParent) {
                gravity = Gravity.CENTER
                weight = 5f
                margin = dip(3)
            }
        }
        linearLayout {
            button {
                text = "/"
            }.lparams(width = wrapContent, height = matchParent) {
                gravity = Gravity.CENTER
                weight = 5f
                margin = dip(3)
            }
            button {
                text = "*"
            }.lparams(width = wrapContent, height = matchParent) {
                gravity = Gravity.CENTER
                weight = 2.5f
                margin = dip(3)
            }
            button {
                text = "-"
            }.lparams(width = wrapContent, height = matchParent) {
                gravity = Gravity.CENTER
                weight = 2.5f
                margin = dip(3)
            }
            button {
                text = "+"
            }.lparams(width = wrapContent, height = matchParent) {
                gravity = Gravity.CENTER
                weight = 2.5f
                margin = dip(3)
            }
        }
        linearLayout {
            button {
                text = "base10"
            }.lparams(width = matchParent, height = wrapContent){
                margin = dip(3)
            }
        }
        linearLayout {
            button {
                text = "calculate!"
            }.lparams(width = matchParent, height = wrapContent){
                margin = dip(3)
            }
        }
        lparams { //This is the culprit!
            paddingTop = dip(200)
        }
    }
}

marginToppaddingTop均无效,但是IDE会在输入marginpadding时建议使用。只是paddingmargin不会提示错误,但是我只想在组件的最顶部腾出空间。

2 个答案:

答案 0 :(得分:1)

使用topPadding代替paddingTop

答案 1 :(得分:0)

我认为您需要更改:

verticalLayout {
    ...
    lparams {
        ...
    }
}

改为阅读:

verticalLayout {
    ...
}.lparams {
    ...
}

我以前没有使用过Anko库,但是以上更改来自他们在GitHub项目:https://github.com/Kotlin/anko/issues/392#issuecomment-304659964

中的注释