唯客微博客

专注于计算机,嵌入式领域的技术

0%

Jetpack Compose组件-标签页(Tab)

Jetpack Compose组件-标签页(Tab)

标签(Tab)

Tab图标位于文字上方,LeadingIconTab图标位于文字左侧。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
@Composable
fun Tab(
// 是否选中
selected: Boolean,
// 点击回调
onClick: () -> Unit,
// 修饰符
modifier: Modifier = Modifier,
// tab使能
enabled: Boolean = true,
// 点击状态源
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
// 选中内容颜色
selectedContentColor: Color = LocalContentColor.current,
// 未选中内容颜色
unselectedContentColor: Color = selectedContentColor.copy(alpha = ContentAlpha.medium),
content: @Composable ColumnScope.() -> Unit
): Unit

@Composable
fun Tab(
// 是否选中
selected: Boolean,
// 点击回调
onClick: () -> Unit,
// 修饰符
modifier: Modifier = Modifier,
// tab使能
enabled: Boolean = true,
// 文本
text: (@Composable () -> Unit)? = null,
// 图标
icon: (@Composable () -> Unit)? = null,
// 点击状态源
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
// 选中内容颜色
selectedContentColor: Color = LocalContentColor.current,
// 未选中内容颜色
unselectedContentColor: Color = selectedContentColor.copy(alpha = ContentAlpha.medium)
): Unit

@Composable
fun LeadingIconTab(
// 是否选中
selected: Boolean,
// 点击回调
onClick: () -> Unit,
// 文本
text: @Composable () -> Unit,
// 图标
icon: @Composable () -> Unit,
// 修饰符
modifier: Modifier = Modifier,
// tab使能
enabled: Boolean = true,
// 点击状态源
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
// 选中内容颜色
selectedContentColor: Color = LocalContentColor.current,
// 未选中内容颜色
unselectedContentColor: Color = selectedContentColor.copy(alpha = ContentAlpha.medium)
): Unit

标签行(TabRow)

ScrollableTabRow支持滚动。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
@Composable
@UiComposable
fun TabRow(
// 当前选中的 Tab 下标
selectedTabIndex: Int,
// 修饰符
modifier: Modifier = Modifier,
// 背景颜色
backgroundColor: Color = MaterialTheme.colors.primarySurface,
// 内容颜色
contentColor: Color = contentColorFor(backgroundColor),
// 指示器
indicator: @Composable @UiComposable (tabPositions: List<TabPosition>) -> Unit = @Composable { tabPositions ->
TabRowDefaults.Indicator(
Modifier.tabIndicatorOffset(tabPositions[selectedTabIndex])
)
},
// 底部分割线
divider: @Composable @UiComposable () -> Unit = @Composable {
TabRowDefaults.Divider()
},
// Tab 数组
tabs: @Composable @UiComposable () -> Unit
): Unit

@Composable
@UiComposable
fun ScrollableTabRow(
// 当前选中的 Tab 下标
selectedTabIndex: Int,
// 修饰符
modifier: Modifier = Modifier,
// 背景颜色
backgroundColor: Color = MaterialTheme.colors.primarySurface,
// 内容颜色
contentColor: Color = contentColorFor(backgroundColor),
// 第一个tab距离左侧以及最后一个tab距离右侧的padding
edgePadding: Dp = TabRowDefaults.ScrollableTabRowPadding,
// 指示器
indicator: @Composable @UiComposable (tabPositions: List<TabPosition>) -> Unit = @Composable { tabPositions ->
TabRowDefaults.Indicator(
Modifier.tabIndicatorOffset(tabPositions[selectedTabIndex])
)
},
// 底部分割线
divider: @Composable @UiComposable () -> Unit = @Composable {
TabRowDefaults.Divider()
},
// Tab 数组
tabs: @Composable @UiComposable () -> Unit
): Unit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
@Composable
fun SliderTabs() {
var selectedIndex by remember { mutableStateOf(0) }
val bgColor = Color(0xff1E76DA)

val list = listOf("Active", "Completed")
TabRow(
selectedTabIndex = selectedIndex,
backgroundColor = Color(0xff1E76DA),
modifier = Modifier
.clip(RoundedCornerShape(50)),
indicator = { tabPositions ->
TabRowDefaults.Indicator(
modifier = Modifier
.tabIndicatorOffset(tabPositions[selectedIndex])
.fillMaxSize()
.padding(1.dp)
.clip(RoundedCornerShape(50)),
color = Color.White
)
}
) {
list.forEachIndexed { index, text ->
val selected = selectedIndex == index
LeadingIconTab(
modifier = Modifier.zIndex(2f),
selected = selected,
onClick = { selectedIndex = index },
selectedContentColor = bgColor,
unselectedContentColor = Color.White,
text = { Text(text = text) },
icon = { Icon(imageVector = Icons.Default.Favorite, contentDescription = null) },
interactionSource = NoRippleInteractionSource()
)
}
}
}
-------------本文结束感谢您的阅读-------------

本文标题:Jetpack Compose组件-标签页(Tab)

文章作者:Vinx

发布时间:2023年01月14日 - 14:06

最后更新:2023年09月19日 - 08:41

原始链接:https://blog.vinkvin.com/post/76/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。