1. 程式人生 > >Anko:Android 程式碼動態佈局的新方案

Anko:Android 程式碼動態佈局的新方案

Anko版本的 hello world :

Anko Layouts is a DSL for writing dynamic Android layouts. Here is a simple UI written with Anko DSL:

verticalLayout {
val name = editText()
button(“Say Hello”) {
onClick { toast(“Hello, ${name.text}!”) }
}
}

The code above creates a button inside a LinearLayout and attaches an OnClickListener to that button. Moreover, onClick accepts a suspend lambda, so you can write your asynchronous code right inside the listener!

Note that this is the complete layout code. No XML is required!