Anko 0.8 - unresolved lparams reference

2019-05-06 23:12发布

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?

标签: kotlin anko
2条回答
ゆ 、 Hurt°
2楼-- · 2019-05-06 23:59

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.

查看更多
贪生不怕死
3楼-- · 2019-05-07 00:10

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 
    } 
}
查看更多
登录 后发表回答