Anko 0.8 - unresolved lparams reference

 ̄綄美尐妖づ 提交于 2019-12-07 02:57:49

问题


The main question is: is lparams simply gone from Anko, or am I doing something terribly wrong? The following snippet fails to compile:

verticalLayout {
}.lparams(width = matchParent, height = matchParent) {
    topMargin = dip(10)
}

While this works without any problems:

verticalLayout {
    layoutParams = LinearLayout.LayoutParams(matchParent, matchParent).apply {
        topMargin = dip(10)
    }
}

I wouldn't mind the second option too much, but you have to specify the layout type when generating the params, which can get a bit tiresome (and is also more brittle than the original solution).

I haven't found anything on the Anko GitHub page, the changelog, or by skimming recent commits. Here's the full UI class for reference:

class ReviewsFragmentUi(ctx: AnkoContext<ReviewsFragment>) : AnkoComponent<ReviewsFragment> {
    override fun createView(ui: AnkoContext<ReviewsFragment>) = ui.apply {
        verticalLayout {
            layoutParams = LinearLayout.LayoutParams(matchParent, matchParent).apply {
                topMargin = dip(10)
            }
        }
    }.view
}

Relevant Gradle entries (I'm using Kotlin 1.0.0-beta-3595):

ext.versions = [
    anko : '0.8.1',
]

compile "org.jetbrains.anko:anko-common:$versions.anko",
compile "org.jetbrains.anko:anko-sdk21:$versions.anko",
compile "org.jetbrains.anko:anko-support-v4:$versions.anko",
compile "org.jetbrains.anko:anko-design:$versions.anko",
compile "org.jetbrains.anko:anko-appcompat-v7:$versions.anko",
compile "org.jetbrains.anko:anko-cardview-v7:$versions.anko",
compile "org.jetbrains.anko:anko-recyclerview-v7:$versions.anko",
compile "org.jetbrains.anko:anko-gridlayout-v7:$versions.anko",

As a follow-up question: if lparams is indeed gone, then is there a more elegant replacement than what I'm already doing?


回答1:


Apparently lparams is still there, but cannot be used as an extension function for the outermost layout:

So the following code fails:

override fun createView(ui: AnkoContext<ReviewsFragment>) = ui.apply {
    verticalLayout {
        // Layout elements here
    }.lparams { 
        // Layout params here
    }
}.view

But this compiles fine:

override fun createView(ui: AnkoContext<ReviewsFragment>) = ui.apply {
    verticalLayout {
        lparams {
            // Layout params here
        }

        // Layout elements here
        verticalLayout { }.lparams {
            // lparams works fine if there is a parent layout
        }
    } 
}.view

It's worth noting that using the non-tailing version of lparams is discouraged for inner layouts: it will create the wrong sublass of LayoutParams when the nested layouts are of different types. For a complete discussion, see this GitHub Issue.




回答2:


Why don't you use the most recent way to write createView() method? I think the following solves your problem:

override fun createView(ui: AnkoContext<ReviewsFragment>) : View = with(ui) { 
    return verticalLayout { 
    // Layout elements here 
    }.lparams { 
    // Layout params here 
    } 
}


来源:https://stackoverflow.com/questions/34351584/anko-0-8-unresolved-lparams-reference

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!