“React Native: ‘top’属性表现如预期,但’bottom’则不是”

huangapple go评论77阅读模式
英文:

React Native: 'top' property behaving as expected, but 'bottom' isn't

问题

以下是您React Native应用程序中的代码:

<View
  style={{
    width: 50,
    height: 50,
    borderWidth: 1,
  }}
>
  <View style={{
    width: 5,
    height: 5,
    backgroundColor: 'red',
    top: 10,
    left: 10
  }}></View>
</View>

如预期,这将导致以下结果:

“React Native: ‘top’属性表现如预期,但’bottom’则不是”

然而,如果我将 top 替换为 bottom,我会得到以下结果:

“React Native: ‘top’属性表现如预期,但’bottom’则不是”

如果我将子元素更改为 position: absolute,它将按预期工作:

“React Native: ‘top’属性表现如预期,但’bottom’则不是”

我想知道的是:

  1. 为什么第二张图中红色点在黑色正方形上方?

  2. 由于红色点是黑色正方形的唯一子元素,为什么添加 position: absolute 会改变任何内容?

  3. 为什么 top 在三张图片中都表现如预期,但 bottom 仅在第三张图片中表现如预期?

英文:

I have the following code in my React Native app:

&lt;View
  style={{
    width: 50,
    height: 50,
    borderWidth: 1,
  }}
&gt;
  &lt;View style={{
    width: 5,
    height: 5,
    backgroundColor: &#39;red&#39;,
    top: 10,
    left: 10
  }}&gt;&lt;/View&gt;
&lt;/View&gt;

As expected, this results in:

“React Native: ‘top’属性表现如预期,但’bottom’则不是”

However, if I swap top for bottom, I get this:

“React Native: ‘top’属性表现如预期,但’bottom’则不是”

If I change the child element to position: absolute, it works as expected:

“React Native: ‘top’属性表现如预期,但’bottom’则不是”

What I Want To Know:

  1. Why does the red dot go above the black square in the second image?

  2. Since the red dot is the only child of the black square, why does adding position: absolute change anything?

  3. Why does top behave as expected in all three images, but bottom behaves as expected only in the third?

答案1

得分: 2

在React-Native中,每个布局元素默认都是相对定位的,因此距离容器初始位置的底部相对位置为10px,这是正确的行为。

如果要将小圆点相对于容器的边界定位,将子元素的位置设置为绝对定位。

<View
  style={{
    width: 50,
    height: 50,
    borderWidth: 1,
    position: 'relative' // 默认情况下是相对定位
  }}
>
  <View
    style={{
      width: 5,
      height: 5,
      backgroundColor: "red",
      bottom: 10,
      left: 10,
      position: 'absolute'
    }}
  />
</View>
英文:

In React-Native, every layout element is relatively positioned by default, so 10px relatively from the bottom of initial position lays out of the container, it is proper behavior.

Set the child's position to absolute, if You want to position the dot against the bounds of the container.

    &lt;View
      style={{
        width: 50,
        height: 50,
        borderWidth: 1,
        position: &#39;relative&#39; // by default anyway
      }}
    &gt;
      &lt;View
        style={{
          width: 5,
          height: 5,
          backgroundColor: &quot;red&quot;,
          bottom: 10,
          left: 10,
          position: &#39;absolute&#39;
        }}
      /&gt;
    &lt;/View&gt;

答案2

得分: 2

  1. 因为没有指定position: absolute,所有位置命令都是相对于元素初始位置的。

  2. 它改变了位置命令(top、left、right、bottom)相对于父元素的解释方式。

  3. 因为top无论position如何设置,都指向相同位置,但bottom可能根据position设置为相对或绝对而指向不同位置。

英文:
  1. Because without specifying position: absolute all position commands are relative to where the element is initially.

  2. It changes how the positions commands (top, left, right, bottom ) are interpreted relative to the parent element

  3. Because top refers to the same place regardless of what position is set to, but buttom could refer to a different place depending on if you have position set to relative or absolute

huangapple
  • 本文由 发表于 2020年1月7日 00:27:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615612.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定