React Native 绝对定位
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>
);
}
因为我使用的是绝对定位,所以我希望三个方块位于彼此之上的同一位置。但我得到的是:
我可以在没有任何自动布局的情况下将三个方块准确定位到我要求的位置吗?
是的,你可以,你必须让每一个都绝对定位。
<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>
你做错了你需要将position:absolute
应用到每个方块
试试这个:
<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>
问题是您的父视图是绝对定位的,但随后每个子视图都自动布置在 所述父视图内。您应该对每个需要定位的元素应用绝对定位。
<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>
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>
);
}
因为我使用的是绝对定位,所以我希望三个方块位于彼此之上的同一位置。但我得到的是:
我可以在没有任何自动布局的情况下将三个方块准确定位到我要求的位置吗?
是的,你可以,你必须让每一个都绝对定位。
<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>
你做错了你需要将position:absolute
应用到每个方块
试试这个:
<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>
问题是您的父视图是绝对定位的,但随后每个子视图都自动布置在 所述父视图内。您应该对每个需要定位的元素应用绝对定位。
<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>