简体   繁体   English

为什么从顶部填充根据元素的 alignment 获得不同的长度值?

[英]why padding from top gets different length value based on element's alignment?

Lets say i have two screnario have divider's that get padding from top.可以说我有两个场景有分隔符,从顶部填充。

First scenario (Aligment.TopCenter);第一种情况(Aligment.TopCenter);

Box(
                    modifier = Modifier.fillMaxSize(),
                    contentAlignment = Alignment.TopCenter
                ) {
                    Divider(
                        thickness = 3.dp,
                        color = Color.Red)
                    Divider(
                        modifier = Modifier.padding(top = 200.dp),
                        thickness = 3.dp,
                        color = Color.Black)
                }

Screen:屏幕:

在此处输入图像描述

Second scenario (Aligment.Center);第二种情况(Aligment.Center);

Box(
                    modifier = Modifier.fillMaxSize(),
                    contentAlignment = Alignment.Center
                ) {
                    Divider(
                        thickness = 3.dp,
                        color = Color.Red)

                    Divider(
                        modifier = Modifier.padding(top = 200.dp),
                        thickness = 3.dp,
                        color = Color.Black)
                }
            }

Screen:屏幕:

在此处输入图像描述

Question is: Why a is not equal to b even though both have same padding?问题是:为什么 a 不等于 b,即使两者具有相同的填充?

Modifier.padding increases the size of the view. Modifier.padding增加视图的大小。 And when it is placed in the center of the Box , it looks like only half of the padding is applied, but the other half of the view is in the top half of the screen.当它放在Box的中心时,看起来只应用了一半的填充,但视图的另一半位于屏幕的上半部分。

You can add border to see what's actually going on here:您可以添加边框以查看此处实际发生的情况:

Box(
    modifier = Modifier.fillMaxSize(),
    contentAlignment = Alignment.Center
) {
    Divider(
        thickness = 3.dp,
        color = Color.Red)

    Divider(
        modifier = Modifier.border(2.dp, color = Color.Green).padding(top = 200.dp),
        thickness = 3.dp,
        color = Color.Black)
}

You can use Modifier.offset instead to get the result you expect您可以改用Modifier.offset来获得您期望的结果

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

相关问题 Android MediaPlayer的getDuration()方法从元数据而不是实际媒体长度获取值 - Android MediaPlayer's getDuration() method gets value from metadata rather than actual media length 从AlertDialog中删除顶部填充? - Remove top padding from AlertDialog? 填充剪辑缩放元素在顶部,但不是(真的)在侧面 - Padding clips scaled element on top, but not (really) on the side 如何从下到上更改Android Snackbar的初始对齐? - How do I change an Android Snackbar's initial alignment from bottom to top? 对于相同的String,SQLite的length是否会返回与Java的length方法不同的值? - For a same String, will SQLite's length will ever return a different value than Java's length method? 在TextView上调用setText()后,按钮的文本对齐错误 - Button's text gets wrong alignment after a setText() call on a TextView 如何在Gridlayout drawbletop中从顶部开始填充 - how to give the padding from top in Gridlayout drawbletop 为什么默认对象的hashCode在不同的设备中返回不同的值? - Why default object's hashCode returns different value in different devices? 如何根据对象的字段设置列表项的不同顶行和底行? - How do I set different top and bottom lines of a list item based on an object's fields? Android,从元素中删除边距和填充 - Android, remove margin and padding from element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM