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

相关推荐
奕鼎竜瑆7 小时前
Solid 前端响应式开发从零到精通
前端·人工智能
动恰客流统计7 小时前
景区客流统计怎么做?兼顾管控与运营的实施方案解析
大数据·前端·人工智能
派小心.8 小时前
A/B测试工具免费版够用吗?
前端·数据分析
前端snow8 小时前
ai agent --- redis 缓存
前端
duanshu_9 小时前
官网表单与下载异常怎么排查?五个环节定位故障
前端·自动化·flyweight
Zhou14113610 小时前
SpringBoot_03_Web开发
前端·spring boot·python
辻弋20110 小时前
【无标题】
大数据·服务器·前端·搜索引擎·开源软件
FluxArt10 小时前
GPT Image 2.5 中文提示词教程:Flux Art 可复用模板
前端·gpt·算法
搬砖的小码农_Sky11 小时前
AI Agent:Windows上Node.js安装后 npm -v报错
前端·npm·node.js·人机交互
计算机魔术师11 小时前
半年翻倍!OpenAI再融300亿,估值冲到1.4万亿
前端