二:IconButton的用法
IconButton的构造方法如下
@Composable
fun IconButton(
onClick: () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
content: @Composable () -> Unit
){
…
}
-
onClick 点击回调
-
modifier 修饰符
-
enabled 是否可用
-
content 该控件包含的内容
-
interactionSource 跟Button一样,可以根据不同状态去处理一些逻辑。比如按下时候如何,没有按下时候如何。
如下举例。比如IconButton是一个左边是+的icon,右边是文本的控件。该文本如果按下时候是显示减少,不按下显示添加。
@Preview()
@Composable
fun iconButtonTest(){
val context = LocalContext.current
val interactionSource = remember {
MutableInteractionSource()
}
val pressState = interactionSource.collectIsPressedAsState()
val pressText = if(pressState.value) “减少” else “添加”
Column(modifier = Modifier.padding(10.dp,10.dp)) {
IconButton(
onClick = {
Toast.makeText(context,“点击了添加”,Toast.LENGTH_SHORT).show()
},
modifier = Modifier.size(80.dp,40.dp).clip(RoundedCornerShape(20)),
enabled = true,
interactionSource = interactionSource
){
Row(verticalAlignment=Alignment.CenterVertically) {
Icon(imageVector = Icons.Filled.Add, contentDescription = “添加的按钮”,tint = Color.Red)
Text(text = pressText,fontSize = 8.sp)
}
}
}
}
三:FloatingActionButton和ExtendedFloatingActionButton的用法
FloatingActionButton是Material风格的控件,默认是浮动在右下角的圆形控件。FloatingActionButton的构造函数方法如下:
@Composable
fun FloatingActionButton(
onClick: () -> Unit,
modifier: Modifier = Modifier,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
shape: Shape = MaterialTheme.shapes.small.copy(CornerSize(percent = 50)),
backgroundColor: Color = MaterialTheme.colors.secondary,
contentColor: Color = contentColorFor(backgroundColor),
elevation: FloatingActionButtonElevation = FloatingActionButtonDefaults.elevation(),
content: @Composable () -> Unit
){
…
}
-
onClick 点击回调
-
modifier 修饰符
-
interactionSource 跟上面一样,处理不同状态的
-
shape 形状
-
backgroundColor 背景颜色
-
contentColor内容颜色
-
elevation阴影
-
content 内容控件(其实ExtendedFloatingActionButton就是在该内容里添加了一个Row,Row里放了个文本跟Icon)
@Preview()
@Composable
fun floatingActionButtonTest(){
val context = LocalContext.current
val interactionSource = remember {
MutableInteractionSource()
}
val pressState = interactionSource.collectIsPressedAsState()
val pressText = if(pressState.value) “减少” else “添加”