Create a custom View/ViewGroup class in Anko DSL

前端 未结 2 1065
我在风中等你
我在风中等你 2021-02-05 18:34

I want to create a custom View which is just a wrapper of some Android Views. I looked into creating a custom ViewGroup which manages the layout of it\'s child views, but I don\

2条回答
  •  忘掉有多难
    2021-02-05 18:47

    I was looking for something like this too, but the most optimal solution i found for custom views is something like this:

    public inline fun ViewManager.customLayout(theme: Int = 0) = customLayout(theme) {}
    public inline fun ViewManager.customLayout(theme: Int = 0, init: CustomLayout.() -> Unit) = ankoView({ CustomLayout(it) }, theme, init)
    
    class CustomLayout(c: Context) : LinearLayout(c) {
        init {
            addView(textView("Some text"))
            addView(textView("Other text"))
        }
    }
    

提交回复
热议问题