android - Jetpack Compose BottomNavBar 标签重叠图标

标签 android android-jetpack-compose android-bottomnav jetpack-compose-navigation

我试图实现jetpack compose bottomnavbar。但是我遇到了这个问题。每当标签没有足够的空间时,它就会与图标重叠。我错过了什么吗?是否有任何解决方案,例如自动截断或缩小文本?
compose_version = '1.0.0-beta09'
[1]: /image/gmD34.png
我的代码

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val navController = rememberNavController()
            val items = listOf(
                Screen.Profile,
                Screen.FriendsList,
                Screen.Notification,
                Screen.Setting
            )

            Scaffold(
                bottomBar = {
                    BottomNavigation(
                        backgroundColor = MaterialTheme.colors.surface,
                        elevation = 8.dp
                    ) {
                        val navBackStackEntry by navController.currentBackStackEntryAsState()
                        val currentRoute = navBackStackEntry?.destination?.route
                        items.forEachIndexed { index, screen ->
                            BottomNavigationItem(
                                icon = {
                                    when (index) {
                                        0 -> Icon(Icons.Filled.Person, "Profile")
                                        1 -> Icon(
                                            painterResource(id = R.drawable.ic_friends),
                                            "Friends"
                                        )
                                        2 -> Icon(Icons.Filled.Notifications, "Notification")
                                        else -> Icon(Icons.Filled.Settings, "Settings")
                                    }
                                },
                                label = { Text(stringResource(screen.resourceId)) },
                                selected = currentRoute == screen.route,
                                selectedContentColor = MaterialTheme.colors.primary,
                                unselectedContentColor = Color.Black,
                                onClick = {
                                    navController.navigate(screen.route) {
                                        navController.graph.startDestinationRoute?.let {
                                            popUpTo(it) {
                                                saveState = true
                                            }
                                        }
                                        launchSingleTop = true
                                        restoreState = true
                                    }
                                }
                            )

                        }
                    }
                }
            ) {
                NavHost(navController, startDestination = Screen.Profile.route) {
                    composable(Screen.Profile.route) { Profile(navController) }
                    composable(Screen.FriendsList.route) { FriendsList(navController) }
                    composable(Screen.Notification.route) { FriendsList(navController) }
                    composable(Screen.Setting.route) { FriendsList(navController) }
                }
                
            }
        }
    }

最佳答案

您可以添加属性 maxLines = 1Text用于label :

BottomNavigationItem(
      /* your code */
      label = { Text("Notification",
               maxLines = 1,
               overflow = TextOverflow.Ellipsis) /* optional */
      }
)
enter image description here

关于android - Jetpack Compose BottomNavBar 标签重叠图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68025499/

相关文章:

android - 底部导航栏在尝试滚动时隐藏

android - 带 fragment 的底部导航栏

java - 底部导航栏在 fragment 内部不起作用

java - 当带有预填充文本的 EditText 变为可编辑时,所有文本移动到一行

java - GridView 中使用 Glide 加载图像太慢

android - 如何在 Android Jetpack Compose 中创建可组合项的多个预览?

android - 为什么我们不能在 jetpack compose 的 mutableStateListOf 中使用 delegate 属性

kotlin - SnapshotStateList 如何检测发生了更改?

android - Delphi Seattle 无法引用默认的 FMX 类

android - 使用 LoopJ Android syncHttp 从 https 下载图片资源