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 “添加”
Column(modifier = Modifier.padding(10.dp,10.dp)) {
FloatingActionButton(
onClick = {
Toast.makeText(context,“点击了按钮”,Toast.LENGTH_SHORT).show()
},
modifier = Modifier.size(80.dp),
interactionSource = interactionSource,
shape = RoundedCornerShape(25.dp),
backgroundColor = Color.Green,
contentColor = Color.Blue,
elevation = FloatingActionButtonDefaults.elevation(defaultElevation=8.dp,pressedElevation = 10.dp)
){
Row(verticalAlignment=Alignment.CenterVertically) {
Icon(imageVector = Icons.Filled.Add, contentDescription = “Add”,tint = Color.Red)
Spacer(modifier = Modifier.width(10.dp))
Text(text = “按钮”,fontSize =12.sp,color = Color.White)
}
}
}
ExtendedFloatingActionButton是FloatingActionButton的扩展 ExtendedFloatingActionButton的构造方法如下
@Composable
fun ExtendedFloatingActionButton(
text: @Composable () -> Unit,
onClick: () -> Unit,
modifier: Modifier = Modifier,
icon: @Composable (() -> Unit)? = null,
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()
) {
…
}
-
text显示的文本控件
-
onClick 点击回调
-
modifier 修饰符
-
icon 显示的icon控件
-
interactio