為什么不使用xml繪制Andoird的UI?
- 型別不安全
- 非空不安全
- xml迫使你在很多布局中寫很多相同的代碼
- 設備在決議xml時會耗費更多的cpu運行時間和電池
- 最重要的時,它允許任何代碼重用
簡單案例
val act = this
val layout = LinearLayout(act)
layout.orientation = LinearLayout.VERTICAL
val name = EditText(act)
val button = Button(act)
button.text = "Say Hello"
button.setOnClickListener {
Toast.makeText(act, "Hello, ${name.text}!", Toast.LENGTH_SHORT).show()
}
layout.addView(name)
layout.addView(button)
verticalLayout {
val name = editText()
button("Say Hello") {
onClick { toast("Hello, ${name.text}!") }
}
}
findViewById()
val name = find<TextView>(R.id.name)
name.hint = "請輸入你的姓名"
name.onClick{Toast(name.text)}
LayoutParams
使用lparams()描述布局引數,其中寬高都是wrapContent
linearLayout {
button("Login") {
textSize = 26f
}.lparams(width = wrapContent) {
horizontalMargin = dip(5)
topMargin = dip(10)
}
}
horizontalMargin設定左右的邊距verticalMargin設定上下的邊距margin設定四周的邊距
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/212114.html
標籤:其他
上一篇:編程人生:程式員請放下你的技術情節,與你的同伴一起進步
下一篇:AppleDoc
