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

边栏推荐
- Find - (half find / half find)
- 多级介孔有机金属骨架材料ZIF-8负载乳酸氧化酶(LOD)/四氧化三铁(Fe304)/阿霉素DOX/胰岛素/cas9蛋白/甲硝唑/大黄素甲醚
- Online courses avaiable
- What is the relationship between precious metal silver and spot Silver
- Core principle and code explanation of epoll reactor model
- 软测人都该知道的七大原则
- Secret
- Nodejs send mail
- Record scroll bar position, passive, scrolltop
- Why is the trend chart of precious metal silver strong?
猜你喜欢

QT database learning notes (II) QT operation SQLite database

Find - (block find)

The annual salary of testers in large factories ranges from 300000 to 8K a month. Roast complained that the salary was too low, but he was ridiculed by netizens?

Asemi FET 12n65 parameters, 12n65 specifications, 12n65 features

双面材质【double sided】的Shader

Koa2 learning notes

SQL | 计算总和

Sword finger offer II 095 Longest common subsequence dynamic programming

In the past 10 years, from zero foundation testing to test architect, he has made himself successful

Introduction for i-Teams
随机推荐
2022 simulated 100 questions and answers for crane driver (limited to bridge crane) examination
SAP smartforms text content manual wrap output
14: 00 interview, came out at 14:08, the question is really too
Oracle tablespaces, users, and authorization to users
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
[parallel and distributed systems] cache learning
技术分享| 快对讲,全球对讲
腾讯面试官曰Mysql架构的内部模块索引原理及性能优化思路谁会?
Enrichment of core knowledge points of interface automation to add points to the interview
[penetration test tool bee] how to install and use the XSS penetration test tool bee?
常见漏洞的防御措施整理
关于Set集合类你都知道什么?来自《卷Ⅰ》的灵魂提问
ABAP CDs realizes multi line field content splicing
Day code 300 lines learning notes day 22
Rewrite: kms activates office2016, 2019 and 2021 with error code: 0xc004f069
CRS-4544 & ORA-09925
QT database learning notes (II) QT operation SQLite database
Is it appropriate for a 27 - year-old girl to change her career from zero to software testing?
889. 根据前序和后序遍历构造二叉树