Jetpack是否组成BottomNavBar?怎样才能用bottomNavbar举例?

时间:2019-10-26 20:01:47

标签: android-jetpack-compose

我正在使用Android应用程序中的jectpack compose。所以我想使用use bottomAppbar。从来没有发现任何例子可以有人帮助吗?

2 个答案:

答案 0 :(得分:5)

通过1.0.0-alpha01,您可以使用BottomAppBar

BottomAppBar (backgroundColor = yellow500) {
    IconButton(onClick = { /* doSomething() */ }) {
        Icon(Icons.Filled.Menu)
    }
    // The actions should be at the end of the BottomAppBar
    Spacer(Modifier.weight(1f, true))
    IconButton(onClick = { /* doSomething() */ }) {
        Icon(Icons.Filled.Favorite)
    }
    IconButton(onClick = { /* doSomething() */ }) {
        Icon(Icons.Filled.Call)
    }
}

enter image description here

使用FAB:

val fabShape = CircleShape
val scaffoldState = rememberScaffoldState()

Scaffold(topBar = { },
    bottomBar = {
        BottomAppBar(cutoutShape = fabShape) {
            IconButton(onClick = {
                scaffoldState.drawerState.open()
            }) {
                Icon(Icons.Filled.Menu)
            }
        }
    },
    floatingActionButtonPosition = FabPosition.Center,
    isFloatingActionButtonDocked = true,
    floatingActionButton = {
        FloatingActionButton(
            shape = fabShape,
            onClick = {}
        ) {
            Icon(asset = Icons.Filled.Add)
        }
    }, bodyContent = {
        //bodyContent()
    })

enter image description here

答案 1 :(得分:4)

是的jetpack compose通过简单的BottomAppBar支持FloatingActionButton 查看下面的代码示例,它可以为您提供更多帮助

BottomAppBarWithoutFab

@Composable
fun BottomAppBarNoFab(getMyActionImage: () -> Image, getMyNavigationImage: () -> Image) {
    val someActionImage: Image = getMyActionImage()
    val someNavigationImage: Image = getMyNavigationImage()
    val navigationIcon: @Composable() () -> Unit = {
        AppBarIcon(someNavigationImage) { /* doSomething()*/ }
    }
    val actionData = listOf(someActionImage)
    BottomAppBar(
        navigationIcon = navigationIcon,
        actionData = actionData
    ) { actionImage ->
        AppBarIcon(actionImage) { /* doSomething()*/ }
    }
}

检查BottomAppbarWithoutFab屏幕截图

enter image description here

BottomAppBarWithCutout

@Composable
fun BottomAppBarCutoutFab(
    getMyActionImage: () -> Image,
    getMyNavigationImage: () -> Image
) {
    val someActionImage: Image = getMyActionImage()
    val someNavigationImage: Image = getMyNavigationImage()
    val navigationIcon: @Composable() () -> Unit = {
        AppBarIcon(someNavigationImage) { /* doSomething()*/}
    }
    val actionData = listOf(someActionImage)
    BottomAppBar(
        navigationIcon = navigationIcon,
        fabConfiguration = BottomAppBar.FabConfiguration(cutoutShape = CircleShape) {
            FloatingActionButton(
                color = +themeColor { secondary },
                icon = +imageResource(R.drawable.ic_add_icon),
                onClick = { /** doSomething() */ })
        },
        actionData = actionData
    ) { actionImage ->
        AppBarIcon(actionImage) { /* doSomething()*/  }
    }
}

查看bottomAppbarcutoutFab屏幕截图

enter image description here

检查以下代码,我们如何在@Compose函数中调用

Column(mainAxisAlignment = MainAxisAlignment.End) {
        BottomAppBarNoFab(getMyActionImage = {
            +imageResource(R.drawable.ic_home_icon)
        }, getMyNavigationImage = {
            +imageResource(R.drawable.ic_heart_icon)
        })
    }