LoginSignup
2
5

More than 5 years have passed since last update.

Anko で Custom View

Last updated at Posted at 2017-06-12

Anko で Custom View を作る方法です.
端的にいうと,AnkoContext.createDelegate を使います.
Anko のバージョンは 0.10.1 です.

サンプルコード

UI側

class SampleUI : AnkoComponent<SampleView> {
    companion object {
        val textId = View.generateViewId()
    }

    override fun createView(ui: AnkoContext<SampleView>): View = with(ui) {
        verticalView {
            textView {
                id = textViewId
            }
        }
    }
}

View 側

class SampleView(context : Context) : LinearLayout(context) {
  val textView : TextView

  init {
    val view = SampleUI.createView(AnkoContext.createDelegate(context))
    textView = view.find(SampleUI.textId)
  }
}

View を作りさえしたら,あとはいつもの Android と同じです.

メジャーリリースがくるころには,ドキュメントが整備されてこのあたりもわかりやすくなるのでしょう.

2
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5