Jetpack
Compose组件-列表项(ListItem)
列表项(ListItem)
ListItem(小列表项)类似设置界面中的菜单项,有单行/两行/三行文本三种样式,可添加左侧图标,可设置右侧内容,通常是Icon、CheckBox
或 Switch。
ListItem左侧图标和右侧内容默认为上方对齐,要想居中对齐需要将左侧图标大小(size
+ padding)设置为40dpx56dp,右侧内容高度(height+ padding)设置为56dp。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| @Composable @ExperimentalMaterialApi fun ListItem( modifier: Modifier = Modifier, icon: (@Composable () -> Unit)? = null, secondaryText: (@Composable () -> Unit)? = null, singleLineSecondaryText: Boolean = true, overlineText: (@Composable () -> Unit)? = null, trailing: (@Composable () -> Unit)? = null, text: @Composable () -> Unit ): Unit
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| var switched by remember { mutableStateOf(false) } val onSwitchedChange: (Boolean) -> Unit = { switched = it } Column(modifier = Modifier.background(Color.LightGray)) { ListItem( text = { Text("text") }, icon = { Icon( imageVector = Icons.Default.BrokenImage, contentDescription = null, modifier = Modifier .padding(vertical = 16.dp, horizontal = 8.dp) .size(24.dp) ) }, secondaryText = { Text("secondaryText") }, overlineText = { Text("overlineText") }, trailing = { Switch( checked = switched, onCheckedChange = null, modifier = Modifier .padding(16.dp) .size(24.dp) ) }, modifier = Modifier .background(Color.White) .toggleable( role = Role.Switch, value = switched, onValueChange = onSwitchedChange ) ) }
|