Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom view groups to Anko DSL?

Tags:

kotlin

anko

Anko docs tell us how to add custom views to DSL. But if my custom view is a view group, problems arise.

class MyFrameLayout(context: Context) : FrameLayout(context)

fun ViewManager.myFrameLayout(init: MyFrameLayout.() -> Unit = {}) = ankoView({ MyFrameLayout(it) }, init)

class MyUI : AnkoComponent<Fragment> {
    override fun createView(ui: AnkoContext<Fragment>) = with(ui) {

        myFrameLayout {
            textView("hello").lparams { // error: Unresolved reference: lparams
                bottomMargin = dip(40)
            }
        }
    }
}

but if I change myFrameLayout invocation to frameLayout it works OK. So what's the proper way to make view groups be used with Anko DSL?

like image 217
netimen Avatar asked Feb 26 '16 12:02

netimen


1 Answers

Actually you just have to extend anko and declare your customview then use it in the DSL normally:

public inline fun ViewManager.customView() = customView {}
public inline fun ViewManager.customView(init: CustomView.() -> Unit) = ankoView({ CustomView(it) }, init)

Then use it in the DSL normally

frameLayout {
    customView()
}
like image 151
Eefret Avatar answered Sep 22 '22 17:09

Eefret