响应本机背部图像背景居中



我有一个图像,我试图将一些文本居中。

为此,我使用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>

您需要添加一个额外的视图来包装 ImageBackground 并添加样式属性证明内容并将项目对齐居中,我认为这将消除您的问题。

<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>

最新更新