简体   繁体   English

如何在 Jetpack Compose 中添加边距?

[英]How to add Margin in Jetpack Compose?

How exactly can you add Margin in Jetpack Compose ?您究竟如何在Jetpack Compose中添加边距?

I can see that there is a Modifier for padding with Modifier.padding(...) but I can't seem to find one for margins or am I blind?我可以看到有一个Modifier用于填充Modifier.padding(...)但我似乎无法找到一个用于边距的修饰符或者我是瞎子吗?

Someone guide me please.请有人指导我。

Thank you very much.非常感谢。

You can consider padding and margin as the same thing (imagine it as "spacing").您可以将填充和边距视为同一事物(将其想象为“间距”)。 A padding can be applied twice (or more) in the same composable and achieve the similar behavior you would get with margin+padding.填充可以在同一个可组合中应用两次(或更多),并实现与使用边距+填充相似的行为。 For example:例如:

val shape = CircleShape
Text(
    text = "Text 1",
    style = TextStyle(
        color = Color.White,
        fontWeight = FontWeight.Bold,
        textAlign = TextAlign.Center),
    modifier = Modifier.fillMaxWidth()
        .padding(16.dp)
        .drawBorder(2.dp, MaterialTheme.colors.secondary, shape)
        .drawBackground(MaterialTheme.colors.primary, shape)
        .padding(16.dp)
)

Will result on this:将导致:

在此处输入图像描述

As you can see, the first padding is adding a space between the component and its border.如您所见,第一个padding是在组件与其边框之间添加一个空间。 Then the background and border are defined.然后定义背景和边框。 Finally, a new padding is set to add space between the border and the text.最后,设置了一个新的padding以在边框和文本之间添加空间。

Thinking in terms of padding and margin you refer to the so-called box model that we are used to.关于填充边距的思考,您指的是我们习惯的所谓的框 model There's no a box model in Compose but a sequence of modifiers which is applied to a given composable. Compose 中没有框 model 而是一系列应用于给定可组合项的修饰符。 The trick is that you can apply the same modifier like padding or border multiple times and the order of these matters , for example:诀窍是您可以多次应用相同的修饰符(如填充边框)以及这些事项的顺序,例如:

@Composable
fun PaddingExample() {
    Text(
        text = "Hello World!",
        color = Color.White,
        modifier = Modifier
            .padding(8.dp) // margin
            .border(2.dp, Color.White) // outer border
            .padding(8.dp) // space between the borders
            .border(2.dp, Color.Green) // inner border
            .padding(8.dp) // padding
    )
}

As the result you'll get this composable:结果你会得到这个可组合的:

在此处输入图像描述

This design is well explained in the Modifiers documentation :这个设计在 修改器文档中有很好的解释:

Note: The explicit order helps you to reason about how different modifiers will interact.注意:显式顺序可帮助您推断不同修饰符将如何交互。 Compare this to the view-based system where you had to learn the box model, that margins applied "outside" the element but padding "inside" it, and a background element would be sized accordingly.将此与基于视图的系统进行比较,在该系统中您必须学习框 model,边距应用于元素“外部”但填充“内部”,并且背景元素将相应调整大小。 The modifier design makes this kind of behavior explicit and predictable, and gives you more control to achieve the exact behavior you want.修饰符设计使这种行为明确且可预测,并为您提供更多控制权以实现您想要的确切行为。

You can also use Spacer :您还可以使用Spacer

Spacer(modifier = Modifier.width(10.dp))

It represents an empty space layout, whose size can be defined using Modifier.width , Modifier.height and Modifier.size modifiers.它表示一个空白空间布局,其大小可以使用Modifier.widthModifier.heightModifier.size修饰符来定义。

Suppose you want to add margin between 2 composables, then you can achieve it as假设您想在 2 个可组合项之间添加边距,那么您可以将其实现为

Text(
    text = stringResource(id = R.string.share_your_posters),
    fontSize = 16.sp,
    color = Color.Black
)

Spacer(modifier = Modifier.width(10.dp))

Image(painter = painterResource(id = R.drawable.ic_starts), contentDescription = null)

The margin is different than padding, margin is the space outside the widget, where padding is the distance inside the widget, in old XML you could have decided explicitly which one to use, however the new compose way is different.边距与填充不同,边距是小部件外部的空间,其中填充是小部件内部的距离,在旧的 XML 中,您可以明确决定使用哪一个,但是新的组合方式不同。

How compose treat paddings and margins?如何撰写处理填充和边距?

There is an object which can be set as Parameter to the composable called Modifier , you can use this to do both margins and paddings.有一个 object 可以设置为名为Modifier的组合的参数,您可以使用它来执行边距和填充。

Example of Padding:填充示例:

    Text(
    text = "Test",
    modifier = Modifier
        .padding(16.dp)
        .clickable { }
)

Example of Margin保证金示例

    Text(
    text = "Test",
    modifier = Modifier
        .clickable { }
        .padding(16.dp)
)

As you can see the order makes a difference here in compose, all the modifiers are implemented by order.正如您所看到的,在 compose 中顺序有所不同,所有的修饰符都是按顺序实现的。

You can achieve the same effect as margin with putting your content, that has padding, inside a different composable like Box and make outer composable clickable .您可以通过将具有填充的内容放在不同的可组合项(如Box )中并使外部可组合项clickable来实现与边距相同的效果。 With this approach, inner padded areas will be included in clickable content.使用这种方法,内部填充区域将包含在可点击内容中。

I was also looking for something which should give me a direct option to set margin on a View like TextView.我也在寻找可以让我直接选择在 TextView 之类的视图上设置边距的东西。 But unfortunately we don't have margin support in Jetpack compose.但不幸的是,我们在 Jetpack compose 中没有边距支持。 But the good news is we can still achieve it by using layout container like Box, which allows us to add views like TextView, ImageView etc. So you can add margin to any of the child(TextView) by using padding modifier to the parent(Box).但好消息是我们仍然可以通过使用像 Box 这样的布局容器来实现它,这允许我们添加像 TextView、ImageView 等这样的视图。所以你可以通过使用 padding 修饰符给父级添加边距(TextView)。盒子)。 Here is the code:这是代码:

Box(Modifier.padding(10.dp)) {
    Surface(color = Color.LightGray) {
        Text(text = "Hello $text!", color = Color.Blue,
            modifier = Modifier.padding(16.dp))
    }
}

And the result is:结果是:

在此处输入图像描述

Here I have given 10.dp padding to the box.在这里,我为盒子提供了 10.dp 填充。 Hope it is useful.希望它是有用的。

So what from I can understand after reading the documentation there is no margin modifier as such as the API designer felt it is redundant to give something different name which essentially does the same thing.因此,我在阅读文档后可以理解的是,没有边距修饰符,例如 API 设计者认为给本质上做同样事情的不同名称是多余的。

So let's say you want to apply a margin of 8dp before colouring your container with yellow background and you want the container with a padding of 4dp for the content.因此,假设您想在用黄色背景为容器着色之前应用 8dp 的边距,并且您希望容器的内容填充为 4dp。

Column(modifier = Modifier.padding(all = 8.dp)
                          .background(color = Color.Yellow)
                          .padding(all=4.dp)) {
        Text(text = "Android")
        ...
    }

Here in the above example you can see that I have applied the padding first and after that I have added background colour to the container and finally the last padding.在上面的示例中,您可以看到我首先应用了填充,然后我向容器添加了背景颜色,最后是最后一个填充。 And here's how it looks.这就是它的外观。 Just like we intended.就像我们打算的那样。 在此处输入图像描述

You can achieve a margin effect by using nested Surface elements with padding eg您可以通过使用带填充的嵌套Surface元素来实现边距效果,例如

@Composable
fun MainScreen() {
    Surface(color=Color.Yellow, modifier=Modifier.padding(10.dp)){
        Surface(color=Color.Magenta, modifier=Modifier.padding(30.dp)) {
            Surface(
               color = Color.Green, 
               modifier = Modifier.padding(10.dp).wrapContentSize()) {
               Text(text = "My Dummy Text", color = Color.Black)
            }
        }
    }
}

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

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