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?
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()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With