本文介绍了当屏幕尺寸不同时,我应该如何处理绝对位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 react-native 中使用 ImageBackground 在特定位置显示文本.
I want to use ImageBackground in react-native to show text in a specific location.
当使用resize mode as contains时,如何为不同的屏幕尺寸指定不同的绝对位置?
How can I specify different absolute position for different screen sizes when using resize mode as contain?
https://i.stack.imgur.com/HRUwn.png
<ImageBackground
source={require("../../assets/image.png")}
style={{ width: "100%", height: "100%", position: "relative" }}
resizeMode="contain"
onLayout={(event) => this.setLayout(event.nativeEvent.layout)}
>
<Text
style={{
position: "absolute",
left: 90,
top: 300,
}}
>
Text_1
</Text>
<Text
style={{
position: "absolute",
left: 280,
top: 300,
}}
>
Text_2
</Text>
</ImageBackground>
推荐答案
使用维度给出左右位置.示例如下,
Give left and right positions using dimensions.Example like below,
import { Dimensions } from 'react-native';
const window = Dimensions.get('window');
<View style={{
flexdirection:'row',
justifyContent:'space-between',
marginLeft:(window.width)*0.3,
marginRight:(window.width)*0.5,
top:(window.height)*0.25,
position: "absolute",
}}>
<Text>Text One </Text>
<Text>Text Two </Text>
</View>
这篇关于当屏幕尺寸不同时,我应该如何处理绝对位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!