Create a custom View/ViewGroup class in Anko DSL

前端 未结 2 1017
我在风中等你
我在风中等你 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"))
        }
    }
    
    0 讨论(0)
  • 2021-02-05 19:01

    you can use ViewManager.

    fun ViewManager.swipeLayout() = linearLayout {
      textView {
        text = "Some text"
      }
      textView {
        text = "Another text"
      }
    }
    

    class MainActivity
      verticalLayout {
        textView {
          text = "Something that comes above the swipe"
        }
        swipeLayout {}
    }
    
    0 讨论(0)
提交回复
热议问题