使用Compose的动态布局功能,可以在运行时动态地更新UI元素。以下是一些示例:
@Composable fun DynamicButtonList() { val buttonList = remember { mutableStateListOf("Button 1", "Button 2") } Column { buttonList.forEach { buttonTitle -> Button(onClick = {}) { Text(buttonTitle) } } Button(onClick = { buttonList.add("Button ${buttonList.size + 1}")}) { Text("Add Button") } } }
@Composable fun DynamicText() { var textValue by remember { mutableStateOf("Hello, World!") } Box( modifier = Modifier .background(Color.Gray) .padding(16.dp) .clickable { textValue = "Text Clicked!" } ) { Text( text = textValue, modifier = Modifier.align(Alignment.Center) ) } }
@Composable
fun DynamicList() {
val listItems = remember { mutableStateListOf