Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anko 0.8 - unresolved lparams reference

Tags:

kotlin

anko

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?

like image 617
Gustorn Avatar asked Dec 18 '15 08:12

Gustorn


1 Answers

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.

like image 170
Gustorn Avatar answered Sep 25 '22 13:09

Gustorn