And*_*sen 2 css image reactjs react-native imagebackground
我有一个图像,我尝试将一些文本放在其中。
为此,我使用 ImageBackground 来拥有图像的子元素。
然而,图像在小型设备上被缩小,如果可能的话,全尺寸
当我将文本居中时,它会在整个图像比例之后居中(在可以看到整个图像的大屏幕上,它居中 - 在小屏幕上它的偏移量,因为某些图像被裁剪以适合。
<ImageBackground style={{ width: wp('50%'),
height: hp('50%'),
resizeMode: 'cover',
justifyContent: 'center',
alignItems: 'center' }}
source={require('../assets/images/12.jpg')}>
// if the full image is showing - it's centered, otherwise not!
<Text style={{ color: "red" }}>Centered text</Text>
</ImageBackground>
Run Code Online (Sandbox Code Playgroud)
小智 5
您需要添加一个额外的视图来包装 ImageBackground 并将样式属性 justifyContent 和 alignItems 添加到中心,我认为这将消除您的问题。
<View style={{ flex:1 , justifyContent: 'center', alignItems: 'center' }}>
<ImageBackground
style={{ width: wp('50%'), height: hp('50%'), resizeMode: 'cover', justifyContent: 'center', alignItems: 'center' }}
source={require('../assets/images/12.jpg')}>
// if the full image is showing - it's centered, otherwise not!
<Text style={{ color: "red" }}>Centered text</Text>
</ImageBackground>
</View>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2101 次 |
| 最近记录: |