跳转到内容

TabRow ​

TabRow 是 Miuix 中的导航组件,用于创建可横向滚动的标签页。提供了标准样式和带轮廓(Contour)样式两种变体,适用于内容分类和导航场景。

引入 ​

kotlin
import top.yukonga.miuix.kmp.basic.TabRow // 标准样式
import top.yukonga.miuix.kmp.basic.TabRowWithContour // 带轮廓样式

基本用法 ​

标准样式 ​

kotlin
val tabs = listOf("推荐", "关注", "热门", "精选")
var selectedTabIndex by remember { mutableStateOf(0) }

TabRow(
    tabs = tabs,
    selectedTabIndex = selectedTabIndex,
    onTabSelected = { selectedTabIndex = it }
)

带轮廓样式 ​

kotlin
val tabs = listOf("全部", "照片", "视频", "文档")
var selectedTabIndex by remember { mutableStateOf(0) }

TabRowWithContour(
    tabs = tabs,
    selectedTabIndex = selectedTabIndex,
    onTabSelected = { selectedTabIndex = it }
)

记住滚动位置 ​

kotlin
val tabs = listOf("标签1", "标签2", "标签3", "标签4", "标签5")
var selectedTabIndex by remember { mutableStateOf(3) }
val tabListState = rememberLazyListState()

TabRowWithContour(
    tabs = tabs,
    selectedTabIndex = selectedTabIndex,
    onTabSelected = { selectedTabIndex = it },
    listState = tabListState,
)

属性 ​

TabRow 属性 ​

属性名类型说明默认值是否必须
tabsList<String>标签文本列表-是
selectedTabIndexInt当前选中的标签索引-是
onTabSelected(Int) -> Unit标签选中时的回调函数-是
modifierModifier应用于标签行的修饰符Modifier否
colorsTabRowColors标签行的颜色配置TabRowDefaults.tabRowColors()否
minWidthDp每个标签的最小宽度TabRowDefaults.TabRowMinWidth否
maxWidthDp每个标签的最大宽度TabRowDefaults.TabRowMaxWidth否
heightDp标签行的高度TabRowDefaults.TabRowHeight否
cornerRadiusDp标签的圆角半径TabRowDefaults.TabRowCornerRadius否
itemSpacingDp标签之间的间距9.dp否
contentAlignmentAlignment标签内容的对齐方式Alignment.Center否
listStateLazyListState?标签列表的外部滚动状态null否
interactionSourceMutableInteractionSource?标签项的交互源null否
indicationIndication?标签项的点击反馈效果null否

TabRowWithContour 属性 ​

属性名类型说明默认值是否必须
tabsList<String>标签文本列表-是
selectedTabIndexInt当前选中的标签索引-是
onTabSelected(Int) -> Unit标签选中时的回调函数-是
modifierModifier应用于标签行的修饰符Modifier否
colorsTabRowColors标签行的颜色配置TabRowDefaults.tabRowColors()否
minWidthDp每个标签的最小宽度TabRowDefaults.TabRowWithContourMinWidth否
maxWidthDp每个标签的最大宽度TabRowDefaults.TabRowWithContourMaxWidth否
heightDp标签行的高度TabRowDefaults.TabRowWithContourHeight否
cornerRadiusDp标签的圆角半径TabRowDefaults.TabRowWithContourCornerRadius否
itemSpacingDp标签之间的间距5.dp否
contentAlignmentAlignment标签内容的对齐方式Alignment.Center否
listStateLazyListState?标签列表的外部滚动状态null否
interactionSourceMutableInteractionSource?标签项的交互源null否
indicationIndication?标签项的点击反馈效果null否

TabRowDefaults 对象 ​

TabRowDefaults 对象提供了 TabRow 组件的默认配置。

常量 ​

常量名类型值说明
TabRowHeightDp42.dp标准样式的默认高度
TabRowWithContourHeightDp45.dp带轮廓样式的默认高度
TabRowCornerRadiusDp12.dp标准样式的默认圆角半径
TabRowWithContourCornerRadiusDp8.dp带轮廓样式的默认圆角半径
TabRowMinWidthDp76.dp标准样式的每个标签最小宽度
TabRowWithContourMinWidthDp62.dp带轮廓样式的每个标签最小宽度
TabRowMaxWidthDp98.dp标准样式的每个标签最大宽度
TabRowWithContourMaxWidthDp84.dp带轮廓样式的每个标签最大宽度

方法 ​

方法名类型说明
tabRowColors()TabRowColors创建标签行的默认颜色配置

TabRowColors 类 ​

属性名类型说明
backgroundColorColor标签的默认背景色
contentColorColor标签的默认内容色
selectedBackgroundColorColor选中状态时标签的背景色
selectedContentColorColor选中状态时标签的内容色

进阶用法 ​

自定义颜色 ​

kotlin
val tabs = listOf("最新", "热门", "关注")
var selectedTabIndex by remember { mutableStateOf(0) }

TabRow(
    tabs = tabs,
    selectedTabIndex = selectedTabIndex,
    onTabSelected = { selectedTabIndex = it },
    colors = TabRowDefaults.tabRowColors(
        backgroundColor = Color.LightGray.copy(alpha = 0.5f),
        contentColor = Color.Gray,
        selectedBackgroundColor = MiuixTheme.colorScheme.primary,
        selectedContentColor = Color.White
    )
)

自定义尺寸 ​

kotlin
val tabs = listOf("短视频", "直播", "图文")
var selectedTabIndex by remember { mutableStateOf(0) }

TabRowWithContour(
    tabs = tabs,
    selectedTabIndex = selectedTabIndex,
    onTabSelected = { selectedTabIndex = it },
    minWidth = 100.dp,
    height = 50.dp,
    cornerRadius = 15.dp
)

与 Pager 结合使用 ​

kotlin
val tabs = listOf("页面1", "页面2", "页面3")
val pagerState = rememberPagerState { tabs.size }
val scope = rememberCoroutineScope()

Surface {
    Column {
        TabRow(
            tabs = tabs,
            selectedTabIndex = pagerState.currentPage,
            onTabSelected = { index ->
                scope.launch { pagerState.animateScrollToPage(index) }
            }
        )
        HorizontalPager(
            state = pagerState,
            modifier = Modifier.fillMaxSize()
        ) { page ->
            Box(
                modifier = Modifier.fillMaxSize(),
                contentAlignment = Alignment.Center
            ) {
                Text("页面内容 ${page + 1}")
            }
        }
    }
}

变更日志

基于 Apache-2.0 许可发布