当前位置:网站首页>Jetpack Compose Scaffold和TopAppBar(顶部导航)
Jetpack Compose Scaffold和TopAppBar(顶部导航)
2022-06-11 01:26:00 【ScottePerk】
Jetpack Compose 以下简称JC
JC提供了Scaffold这个组件来实现一些导航的效果,例如顶部菜单,底部导航,旁边的抽屉菜单等等,Scaffold的意思是脚手架,也就是Scaffold这是提供的一个支架,或者说固定了一些控件实现的位置。下面通过实现TopAppBar来说明。
TopAppBar有两种实现方式,下面这个是一种实现方式。
@Composable
public fun TopAppBar(
title: @Composable () -> Unit,
modifier: Modifier,
navigationIcon: @Composable() (() -> Unit)?,
actions: @Composable() (RowScope.() -> Unit),
backgroundColor: Color,
contentColor: Color,
elevation: Dp
): Unit
还有第二种实现方式:
@Composable
public fun TopAppBar(
modifier: Modifier,
backgroundColor: Color,
contentColor: Color,
elevation: Dp,
contentPadding: PaddingValues,
content: @Composable() (RowScope.() -> Unit)
): Unit
两者方式的区别就是,第一种方式是已经做了限制的,内部Icon和文本的位置都有一定的限制,而第二种方式实际上是完全的自定义样式,需要自己手动设置位置,总的来说,还是推荐使用第一种比较方便。下面举例说明。
第一种方式:
看注释,写的非常清楚了,这种方式标题部分非常容易居中对齐,第二种方式可能出现问题。
TopAppBar(
//标题
title = {
Text(
modifier = Modifier.fillMaxSize().wrapContentSize(Alignment.Center),
text = "TODO"
)
},
//导航按钮,一般为返回按钮
navigationIcon = {
IconButton(onClick = {
println("返回") }) {
Icon(imageVector = Icons.Default.ArrowBack, contentDescription = null)
}
},
//其它按钮
actions = {
IconButton(onClick = {
println("分享") }) {
Icon(imageVector = Icons.Default.Share, contentDescription = null)
}
IconButton(onClick = {
println("设置") }) {
Icon(imageVector = Icons.Default.Settings, contentDescription = null)
}
},
//背景色
backgroundColor = Color(0xffffffaa),
//内容颜色
contentColor = Color.Black,
//底部阴影
elevation = 5.dp
)

第二种方式的需要完全自定义控件,特别是标题Text的fillMaxWidth()方法,如果设置为1.0f,那么左边的按钮将看不到,如果写一个比例如0.8又可能出现位置的偏差,还是比较难以看着控制的,可能需要别的约束控件来约束位置,那样的话就比第一种方式麻烦的多。
TopAppBar(contentColor = Color.Black, backgroundColor = Color.Red) {
//返回按钮
IconButton(onClick = {
}) {
Icon(Icons.Filled.ArrowBack, null)
}
Text(text = "TODO", modifier = Modifier.fillMaxWidth(0.9f).wrapContentSize(Alignment.Center))
IconButton(onClick = {
println("分享") }) {
Icon(Icons.Default.Share, null)
}
IconButton(onClick = {
println("设置") }) {
Icon(Icons.Default.Settings, null)
}
}

边栏推荐
- C DataGrid binding data
- 聊聊一个注解实现接口重试
- What is the relationship between precious metal silver and spot Silver
- 多级介孔有机金属骨架材料ZIF-8负载乳酸氧化酶(LOD)/四氧化三铁(Fe304)/阿霉素DOX/胰岛素/cas9蛋白/甲硝唑/大黄素甲醚
- Introduction for i-Teams
- Infinite level classification (or menu) design
- 记录一下我的刷题实录
- Customized redistemplate in redis
- CRS-4544 & ORA-09925
- NFT insider 61:animoca brands holds US $1.5 billion of encrypted assets in 340 investments
猜你喜欢

Sword finger offer II 095 Longest common subsequence dynamic programming

Find - (sequential search)

20n10-asemi medium and small power MOS transistor 20n10

The most complete format description of clang format

【并行与分布式系统】Cache学习

查看Redis内数据,除了命令行和客户端,你还有第三种选择

Learning C language from scratch day 040

叶酸配体的金属有机骨架材料MOFs负载5-氟尿嘧啶,西达本胺,紫杉醇,阿霉素,柔红霉素,布洛芬,喜树碱,姜黄素,藤黄酸等小分子药物

【AI周报】AI与冷冻电镜揭示「原子级」NPC结构;清华、商汤提出「SIM」方法兼顾语义对齐与空间分辨能力

CRS-4544 & ORA-09925
随机推荐
10007. ISBN number
Do tween record
14:00面试,14:08就出来了 ,问的实在是太...
Project records
Find - (half find / half find)
Learning C language from scratch day 040
JS basic part hand exercises
Koa2 learning notes
Blue Bridge Cup: the sixth preliminary round - "temperature recorder"
Enrichment of core knowledge points of interface automation to add points to the interview
The largest kth element in the array
Internet of things final assignment - sleep quality detection system (refined version)
Byte beating client R & D Intern Tiktok side
92. CompletableFuture 实战
Day code 300 lines learning notes day 22
Introduction for i-Teams
During SSH configuration key login, you need to pay attention to whether the private key is set with a password
贵金属白银和现货白银之间是什么关系
Secret
10 years of domestic milk powder counter attack: post-90s nannies and dads help new domestic products counter attack foreign brands