简体   繁体   English

反应原生绝对定位

[英]React native absolute positioning

render() {
    return (
      <View style={{position: 'absolute'}}>
        <View style={{top: 50, width:50, height: 50, backgroundColor:'green'}} ></View>
        <View style={{top: 50, width:50, height: 50, backgroundColor:'blue'}} ></View>
        <View style={{top: 50, width:50, height: 50, backgroundColor:'purple'}} ></View>
      </View>
    );
  }

Since I am using absolute positioning, I expected the three squares to be positioned in the very same spot on top of each other.由于我使用的是绝对定位,因此我希望三个正方形彼此重叠放置在同一个位置。 But what I get is this:但我得到的是:

在此处输入图像描述

Can I get the three squares to be positioned exactly where I asked them without any auto layout?我可以在没有任何自动布局的情况下将三个方块准确定位在我要求它们的位置吗?

Yes you can, you have to make each one of them absolutely positioned.是的,您可以,您必须使它们中的每一个都处于绝对位置。

<View>
    <View style={{position: 'absolute', top: 50, width:50, height: 50, backgroundColor:'green'}} ></View>
    <View style={{position: 'absolute', top: 50, width:50, height: 50, backgroundColor:'blue'}} ></View>
    <View style={{position: 'absolute', top: 50, width:50, height: 50, backgroundColor:'purple'}} ></View>
</View>

you were doing it wrong you need to apply position:absolute to each square您做错了,您需要将position:absolute应用于每个正方形

Try this:尝试这个:

<View>
    <View style={{top: 50, width:50, height: 50, position: 'absolute',backgroundColor:'green'}} ></View>
    <View style={{top: 50, width:50, height: 50,position: 'absolute', backgroundColor:'blue'}} ></View>
    <View style={{top: 50, width:50, height: 50, position: 'absolute',backgroundColor:'purple'}} ></View>
  </View>

The problem is that your parent view is being positioned absolutely but then each child is laid out automatically within said parent.问题是您的父视图是绝对定位的,但是每个子视图都会自动所述父视图中布局。 You should apply absolute positioning to each element that you need to position.您应该对 position 所需的每个元素应用绝对定位。

<View>
    <View style={{position: 'absolute', top: 50, width:50, height: 50, backgroundColor:'green'}} ></View>
    <View style={{position: 'absolute', top: 50, width:50, height: 50, backgroundColor:'blue'}} ></View>
    <View style={{position: 'absolute', top: 50, width:50, height: 50, backgroundColor:'purple'}} ></View>
</View>

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

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