【问题标题】:Is it possible to have a tab which consist of multiple types. (single icon, and text) with jetpack compose是否可以有一个包含多种类型的选项卡。 (单个图标和文本)与 jetpack compose
【发布时间】:2022-06-12 22:09:12
【问题描述】:

我正在尝试创建一个标签:

  • 开头只有一个图标,右边只有文字

Click to see image

我正在使用这个:

TabItem.kt

sealed class TabItem(
val index: Int,
@DrawableRes val icon: Int?,
@StringRes val title: Int,
val screenToLoad: @Composable () -> Unit
){

object Camera: TabItem(0, R.drawable.ic_camera, R.string.empty_string, {
    CameraScreen()
})

object Chat: TabItem(1, null, R.string.chats, {
    ChatScreen()
})

object Status: TabItem(2, null, R.string.status, {
    StatusScreen()
})

object Call: TabItem(3, null, R.string.calls, {
    CallsScreen()
})

}

用户界面

TabRow(
    selectedTabIndex = selectedIndex,
) {

    tabs.forEachIndexed{index, tabItem ->

        Tab(
            selected = index == selectedIndex,
            modifier = modifier.background(MaterialTheme.colors.primary),
            onClick = {
            onPageSelected(tabItem)
        },

            icon = {
                tabItem.icon?.let { painterResource(id = it) }?.let { Icon(painter = it, contentDescription = stringResource(id = R.string.icon)) }
            },

            text = {
            Text(text = stringResource(id = tabItem.title))

        },)
    }
}

问题是:

图标为下面的文字留出了空间..

我只需要标签中的图标以及其他标签的文本..

【问题讨论】:

    标签: android android-studio kotlin android-jetpack-compose


    【解决方案1】:

    我是这样管理的:

    TabRow(selectedTabIndex = 0) {
            Tab(
                selected = true,
                onClick = { },
                text = { Icon(Icons.Filled.AccountCircle, "icon", tint = Color.White).toString() })
            Tab(
                selected = true,
                onClick = { },
                text = {
                    Text(text = "Text")
                })
        }
    

    【讨论】:

    • 很遗憾,还是一样,图标底部还有很大的空间。 (就像我在图片中显示的那样)
    • 嗯,这很奇怪:当我在我的模拟器上尝试它时没有空间......
    • 我其实加了一个:Tab first: Tab( modifier = modifier, selected = true, onClick = { }, text = { Icon(Icons.Filled.AccountCircle, "icon", tint = Color. White).toString() }) 然后,我的 tabs.forEachIndexed{ }
    • 谢谢...我找到了一种将它带入循环的方法,现在它可以工作了。
    • 太棒了,干得好! :-)
    猜你喜欢
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    相关资源
    最近更新 更多