gpt4 book ai didi

android - 如何在不重叠导航项的情况下将 FAB 与 BottomAppBar 结合起来?

转载 作者:行者123 更新时间:2023-12-04 23:56:03 25 4
gpt4 key购买 nike

我在 Jetpack Compose 的应用程序中使用 Material 的 BottomAppBar 作为我的 BottomNav。但是当我尝试将我的工厂停靠在 BottomAppBar 上时,它会覆盖导航项,如屏幕截图所示。有没有什么办法可以自动在fab旁边加个空间?

Screenshot

我想实现这个效果,不需要在导航项之间手动添加Space,如下图效果:

Desired effect

下面是我的代码:

@Composable
fun TestApp() {
val navController = rememberNavController()

Scaffold(
bottomBar = {
MyBottomAppBar(navController = navController)
},
floatingActionButton = {
FloatingActionButton(onClick = { }) {
Icon(imageVector = Icons.Rounded.Add, contentDescription = "fab")
}
},
floatingActionButtonPosition = FabPosition.Center,
isFloatingActionButtonDocked = true,
) {
NavHost(navController, startDestination = Screen.Bill.route) {
composable(Screen.Bill.route) { BillScreen() }
composable(Screen.Chart.route) { ChartScreen() }
composable(Screen.Budget.route) { BudgetScreen() }
composable(Screen.Account.route) { AccountScreen() }
}
}
}
@Composable
fun MyBottomAppBar(navController: NavController) {
BottomAppBar(
cutoutShape = CircleShape
) {
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.arguments?.getString(KEY_ROUTE)

Screen.items.forEachIndexed { index, screen ->
BottomNavigationItem(
selected = (currentRoute == screen.route),
icon = { Icon(screen.iconRes, screen.route) },
label = { Text(stringResource(id = screen.labelRes)) },
onClick = {
navController.navigate(screen.route) {
popUpTo = navController.graph.startDestination
launchSingleTop = true
}
}
)
}
}
}

最佳答案

BottomNavigation 是一个 Row 并且所有 BottomNavigationItem 都是 Box.weight( 1f) RowScope 中的修饰符。

您可以在 RowBottomNavigation 中间添加一个与 BottomNavigationItem 大小相同的“空”元素。

例如:

bottomBar = {
BottomAppBar(cutoutShape = fabShape) {
BottomNavigation {
items.forEachIndexed { index, item ->
if (index != 2){ //
BottomNavigationItem(
// your implementation
)} else {
//Empty BottomNavigationItem
BottomNavigationItem(
icon = {},
label = { },
selected = false,
onClick = { },
enabled = false
)
}
}
}

}
},

enter image description here

关于android - 如何在不重叠导航项的情况下将 FAB 与 BottomAppBar 结合起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67530102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com