Compose 布局和修饰符的基础知识

Fundamentals of Compose Layouts and Modifiers - MAD Skills

了解布局和修饰符的基础知识:布局和修饰符如何协同工作、提供哪些开箱即用型 API,以及如何设置界面样式。

1. Layouts

2. Arrangement and Alignment

Arrangement:main axis(主轴)

Alignment:cross axis(交叉轴)

可以单独设置某个 Compose 元素的排列位置。

Box

Box 使用 contentAlignment 排列 content。

kotlin 复制代码
Box(
    contentAlignment = Alignment.TopStart
) {

}

Modifier scope safety

3. Material Components

4. Scaffold

kotlin 复制代码
@Composable
fun Scaffold(
    modifier: Modifier = Modifier,
    topBar: @Composable () -> Unit = {},
    bottomBar: @Composable () -> Unit = {},
    snackbarHost: @Composable () -> Unit = {},
    floatingActionButton: @Composable () -> Unit = {},
    floatingActionButtonPosition: FabPosition = FabPosition.End,
    containerColor: Color = MaterialTheme.colorScheme.background,
    contentColor: Color = contentColorFor(containerColor),
    contentWindowInsets: WindowInsets = ScaffoldDefaults.contentWindowInsets,
    content: @Composable (PaddingValues) -> Unit
) {
    val safeInsets = remember(contentWindowInsets) { MutableWindowInsets(contentWindowInsets) }
    Surface(
        modifier =
            modifier.onConsumedWindowInsetsChanged { consumedWindowInsets ->
                // Exclude currently consumed window insets from user provided contentWindowInsets
                safeInsets.insets = contentWindowInsets.exclude(consumedWindowInsets)
            },
        color = containerColor,
        contentColor = contentColor
    ) {
        ScaffoldLayout(
            fabPosition = floatingActionButtonPosition,
            topBar = topBar,
            bottomBar = bottomBar,
            content = content,
            snackbar = snackbarHost,
            contentWindowInsets = safeInsets,
            fab = floatingActionButton
        )
    }
}

5. Lazy Components

  • LazyHorizontalGrid

  • LazyColumn

  • LazyRow

  • Staggered grids:

    • LazyVerticalStaggeredGrid

    • LazyHorizontalStaggeredGrid

相关推荐
weixin_427771611 小时前
css font-size 的妙用
前端·css
凤凰战士芭比Q2 小时前
web中间件——Nginx
前端·nginx·中间件
一 乐3 小时前
点餐|智能点餐系统|基于java+ Springboot的动端的点餐系统小程序(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·小程序·论文
bitbitDown3 小时前
从零打造一个 Vite 脚手架工具:比你想象的简单多了
前端·javascript·面试
liangshanbo12154 小时前
CSS 数学函数完全指南:从基础计算到高级动画
前端·css
码上成长5 小时前
GraphQL:让前端自己决定要什么数据
前端·后端·graphql
冴羽5 小时前
为什么在 JavaScript 中 NaN !== NaN?背后藏着 40 年的技术故事
前端·javascript·node.js
久爱@勿忘5 小时前
vue下载项目内静态文件
前端·javascript·vue.js
前端炒粉5 小时前
21.搜索二维矩阵 II
前端·javascript·算法·矩阵
合作小小程序员小小店6 小时前
web网页开发,在线%台球俱乐部管理%系统,基于Idea,html,css,jQuery,jsp,java,ssm,mysql。
java·前端·jdk·intellij-idea·jquery·web