项目需求需要,百度了很多,资料比较少,最后也尝试摸索下了,最终实现了想要的效果。
上图:
上代码:(一些变量复制报错需要自己定义,如:selectedTabIndex 和颜色)
TabRow(
selectedTabIndex = currentTabIndex.value,
backgroundColor = Color.White,
//删除未选中显示的灰色下划线
divider = {},
indicator = { tabPositions ->
TabRowDefaults.Indicator(
//使用系统默认的
modifier = Modifier.tabIndicatorOffset(tabPositions[currentTabIndex.value]),
//选中下划线的颜色
color = RedColor
)
},
) {
tabList.forEachIndexed { index, s ->
Tab(text = {
Text(
text = s,
fontSize = 18.sp,
fontWeight = if (currentTabIndex.value == index) FontWeight.Bold else null,
color = if (currentTabIndex.value == index) RedColor else Color.Black
)
}, selected = currentTabIndex.value == index, onClick = {
currentTabIndex.value = index
}, selectedContentColor = RedColor
)
}
}