简体   繁体   English

Jetpack UI 组合。 如何创建 FloatingActionButton?

[英]Jetpack UI Compose. How to create FloatingActionButton?

I want to create a FloatingActionButton placed at the bottom right in the activity using jetpack compose .我想使用jetpack compose创建一个放置在活动右下角的FloatingActionButton
Can anyone help me with an example for the above scenario?任何人都可以帮我举一个上述场景的例子吗?

With 1.0.x to create a FloatingActionButton or the ExtendedFloatingActionButton you can use something like:使用1.0.x创建FloatingActionButtonExtendedFloatingActionButton您可以使用以下内容:

val onClick = { /* Do something */ }

//Simple FAB
FloatingActionButton(onClick = onClick) {
    Icon(Icons.Filled.Add,"")
}

//FAB custom color
FloatingActionButton(
    onClick = onClick,
    backgroundColor = Color.Blue,
    contentColor = Color.White
){
    Icon(Icons.Filled.Add,"")
}

//Square FAB
FloatingActionButton(
    onClick = onClick,
    shape = RectangleShape
){
    Icon(Icons.Filled.Add,"")
}

//EXTENDED FAB
ExtendedFloatingActionButton(
    text = {  Text(text = "EXTENDED FAB") },
    onClick = onClick,
    icon ={ Icon(Icons.Filled.Add,"")}
)

//EXTENDED FAB WITHOUT ICON
ExtendedFloatingActionButton(
    text = {
        Text(text = "EXTENDED FAB")
    },
    onClick = onClick
)

在此处输入图像描述

Example:例子:

Scaffold(topBar = { } ,
    //floatingActionButtonPosition = FabPosition.End,
    floatingActionButton = {
        FloatingActionButton(
            onClick = {}
        ) {
            Icon(Icons.Filled.Add,"")
        }
    }
    , content = {
        //....
    })

在此处输入图像描述

Use Scaffold使用Scaffold

Scaffold provides slots for the most common top-level Material components, such as TopAppBar, BottomAppBar, FloatingActionButton, and Drawer. Scaffold 为最常见的顶级 Material 组件提供插槽,例如 TopAppBar、BottomAppBar、FloatingActionButton 和 Drawer。 By using Scaffold, it's easy to make sure these components are properly positioned and work together correctly.通过使用 Scaffold,可以轻松确保这些组件正确定位并正确协同工作。

Syntax句法

@Composable
fun HomeContent() {
    Scaffold(
        topBar = {/**/ },
        drawerContent = {/**/ },
        bottomBar = {/**/ },
        floatingActionButton = {/**/ },
        snackbarHost = {/**/ },
        content = {/**/ }
    )
}

Example例子

@Composable
fun HomeContent() {
    Scaffold(
        topBar = {
            TopAppBar(title = { Text("Title") })
        },
        floatingActionButton = {
            FloatingActionButton(
                onClick = { /*TODO*/ },
                backgroundColor = Color.Red,
                content = {
                    Icon(
                        painter = painterResource(id = R.drawable.ic_add),
                        contentDescription = null,
                        tint = Color.White
                    )
                }
            )
        },
        content = {
            Surface(modifier = Modifier.padding(24.dp)) {
                Text(
                    text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
                    fontSize = 16.sp,
                )
            }
        }
    )
}

Output Output

在此处输入图像描述

You can create FloatingActionButton try below @Compose function use your drawable icon.您可以使用您的可绘制图标在@Compose function 下方尝试创建FloatingActionButton

@Composable
fun MyFloatingActionButton() {
      val icon = +imageResource(R.drawable.ic_add_icon)
            FloatingActionButton(icon = icon, color = Color.Red, elevation = 8.dp)
}

If you want to create a FAB from inside your composable you can reuse this composable function:如果你想从你的可组合内创建一个 FAB,你可以重用这个可组合 function:

@Composable
fun FabAnywhere(
    fabPosition: FabPosition,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    content: @Composable () -> Unit
) {
    Scaffold(
        floatingActionButtonPosition = fabPosition,
        floatingActionButton = {
            FloatingActionButton(
                onClick = onClick,
                modifier = modifier,
                content = content
            )
        }
    ) {}
}

Use it like this:像这样使用它:

    FabAnywhere(FabPosition.End, onClick = { }) {
        Icon(Icons.Filled.Add, contentDescription = "Add")
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM