标签:request 开发 预览 close pyw image oss col container
Jetpack Compose 是安卓用于构建UI的一种新方式,采用代码而非xml文件方式,写法与Flutter非常相似。
官方主页:https://developer.android.google.cn/jetpack/compose
官方Demo:https://github.com/android/compose-samples/tree/master/JetNews
官方入门指导: https://developer.android.google.cn/jetpack/compose/tutorial
代码更少
少了xml,少了activity和xml之间的关联.
双向绑定
数据变化了,UI就自动变化.
Text(text = "众鸟高飞尽,孤云独去闲。相看两不厌,只有敬亭山。")
val state = +state { "Text Field to input" }
TextField(
value = state.value,
onValueChange = { state.value = it }
)
Button(text = "咬我啊", onClick = { Log.v("test", "被咬了") })
MaterialTheme {
Column {
val openDialog = +state { false }
Button("Click me", onClick = {
openDialog.value = true
})
if (openDialog.value) {
AlertDialog(
onCloseRequest = {
openDialog.value = false
},
title = {
Text(text = "Alert Dialog Title")
},
text = {
Text("Alert Dialog content")
},
confirmButton = {
Button(
"Confirm",
onClick = {
openDialog.value = false
})
},
dismissButton = {
Button(
"Cancel",
onClick = {
openDialog.value = false
})
},
buttonLayout = AlertDialogButtonLayout.Stacked
)
}
}
}
MaterialTheme {
val checkedState = +state { true }
Switch(
checked = checkedState.value,
onCheckedChange = { checkedState.value = it }
)
}
MaterialTheme {
RadioButton(selected = true,
onSelect = { }
)
}
MaterialTheme {
val checkedState = +state { true }
Checkbox(
checked = checkedState.value,
onCheckedChange = { checkedState.value = it }
)
}
MaterialTheme {
val radioOptions = listOf("A", "B", "C")
val (selectedOption, onOptionSelected) = +state { radioOptions[0] }
RadioGroup(
options = radioOptions,
selectedOption = selectedOption,
onSelectedChange = onOptionSelected
)
}
Container(modifier = Height(300.dp) wraps Expanded) {
Clip(shape = RoundedCornerShape(4.dp)) {
DrawImage(+imageResource(R.drawable.img_0))
}
}
https://github.com/cxyzy1/Jetpack-Compose-Demos
推荐阅读:37岁老码农现身说法:那些年,我走过的弯路
作者:程序园中猿
链接:https://www.jianshu.com/p/4289c2271c95
Android开发UI新技能,你get这个新技能了吗?(附源码详解)
标签:request 开发 预览 close pyw image oss col container
原文地址:https://www.cnblogs.com/Android-Alvin/p/12394076.html