简体   繁体   English

如何使用 Jetpack Compose 创建响应式布局?

[英]How to create responsive layouts with Jetpack Compose?

In the traditional view system, I handled responsiveness by using bias/guidelines/weights/wrap_content/avoiding hard coding dimensions/placing views relative to each other in Constraint Layout, using 'sdp' and 'ssp' etc.在传统的视图系统中,我通过使用偏差/指南/权重/wrap_content/避免硬编码尺寸/在约束布局中相对于彼此放置视图来处理响应性,使用“sdp”和“ssp”等。

How do I create responsive layouts with Jetpack Compose?如何使用 Jetpack Compose 创建响应式布局? I've been searching for info regarding it but unable to find.我一直在搜索有关它的信息,但找不到。

Please help!请帮忙!

Two things can help you build flexible and responsive layouts in compose.有两件事可以帮助您在 compose 中构建灵活且响应式的布局。

1 - Use the Weight modifier in Row and Column 1 - 在行和列中使用权重修饰符

A composable size is defined by the content it's wrapping by default.可组合大小由它默认包装的内容定义。 You can set a composable size to be flexible within its parent.您可以将可组合大小设置为在其父级中灵活。 Let's take a Row that contains two two Box composables.让我们看一个包含两个两个 Box 组合的 Row。 The first box is given twice the weight of the second, so it's given twice the width.第一个盒子的重量是第二个盒子的两倍,所以它的宽度是原来的两倍。 Since the Row is 210.dp wide, the first Box is 140.dp wide, and the second is 70.dp:由于 Row 是 210.dp 宽,第一个 Box 是 140.dp 宽,第二个是 70.dp:

@Composable
fun FlexibleComposable() {
    Row(Modifier.width(210.dp)) {
        Box(Modifier.weight(2f).height(50.dp).background(Color.Blue))
        Box(Modifier.weight(1f).height(50.dp).background(Color.Red))
    }
}

This results in:这导致:

在此处输入图像描述

2 - Use BoxWithConstraints 2 - 使用 BoxWithConstraints

In order to know the constraints coming from the parent and design the layout accordingly, you can use a BoxWithConstraints.为了了解来自父级的约束并相应地设计布局,您可以使用 BoxWithConstraints。 The measurement constraints can be found in the scope of the content lambda.测量约束可以在内容 lambda 的 scope 中找到。 You can use these measurement constraints to compose different layouts for different screen configurations.您可以使用这些测量约束来为不同的屏幕配置组成不同的布局。

It lets you access properties such as the min/max height and width:它允许您访问诸如最小/最大高度和宽度之类的属性:

@Composable
fun WithConstraintsComposable() {
    BoxWithConstraints {
        Text("My minHeight is $minHeight while my maxWidth is $maxWidth")
    }
}

Example usage:示例用法:

BoxWithConstraints {
    val rectangleHeight = 100.dp
    if (maxHeight < rectangleHeight * 2) {
        Box(Modifier.size(50.dp, rectangleHeight).background(Color.Blue))
    } else {
        Column {
            Box(Modifier.size(50.dp, rectangleHeight).background(Color.Blue))
            Box(Modifier.size(50.dp, rectangleHeight).background(Color.Gray))
        }
    }
}

You have also option to manage scalable size of widget and scalable size of font with help of below library.您还可以选择在以下库的帮助下管理小部件的可缩放大小和字体的可缩放大小。 May be that can help you to give option to manage some alternative way.可能这可以帮助您选择管理一些替代方式。

https://github.com/mbpatel6245/cdp https://github.com/mbpatel6245/cdp

https://github.com/mbpatel6245/csp https://github.com/mbpatel6245/csp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM