当前位置:网站首页>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)
}
}

边栏推荐
- Using an old mobile phone to build a server and achieve intranet penetration does not require root (I have personally tested the simplest one many times)
- 【并行与分布式系统】Cache学习
- Number of different paths
- Orcale driver
- About stepping on the pit diary and the association of knowledge points
- Binary tree sequence traversal
- koa2学习笔记
- What should be paid attention to in PMP registration? Special reminder
- Fb02 edit coding block field
- SSH配置密钥登录时需要注意私钥是否设置了密码(passphrase)
猜你喜欢

421. 数组中两个数的最大异或值

2022 safety officer-a certificate special operation certificate examination question bank and simulation examination

可扩/减容线程池C语言原理讲解及代码实现

NFT Insider #61:Animoca Brands 在 340 项投资中持有 15 亿美元的加密资产

Oracle收集统计信息

Test questions and answers of 2022r1 quick opening pressure vessel operation certificate

Unity serial port communication

CRS-4544 & ORA-09925

The diligent is the laziest

技术分享| 快对讲,全球对讲
随机推荐
[3.delphi common components] 7 timer
About stepping on the pit diary and the association of knowledge points
koa2学习笔记
Jump without refresh - detailed explanation of pushstate and replacestate methods in history
Sword finger offer II 095 Longest common subsequence dynamic programming
378. the k-th smallest element in an ordered matrix
Analysis of the difficulties in the architecture design of massive chat messages in the live broadcast room
JS summary of math functions in math objects
Number of different paths
Oracle收集统计信息
1031. 两个非重叠子数组的最大和
During SSH configuration key login, you need to pay attention to whether the private key is set with a password
Why is the trend chart of precious metal silver strong?
What do you know about the set class? Soul questions from Volume I
Software testing interview reply: the technical side is not difficult for me, but the HR side is a hang up
Defense measures for common vulnerabilities
Our understanding of the industrial Internet may be more out of the existing logic
Colab报错:ImportError: cannot import name ‘_check_savefig_extra_args‘ from ‘matplotlib.backend_bases‘
NFT Insider #61:Animoca Brands 在 340 项投资中持有 15 亿美元的加密资产
ME11/ME12采购信息记录及条件记录创建及更新BAPI:ME_INFORECORD_MAINTAIN_MULTI