首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >react原生back ImageBackground居中

react原生back ImageBackground居中
EN

Stack Overflow用户
提问于 2019-05-29 17:25:52
回答 1查看 2.5K关注 0票数 2

我有一张图片,我试着在它上面居中一些文本。

为此,我使用ImageBackground来拥有图像的子元素。

然而,图像在小型设备上被缩小了,如果可能的话,是全尺寸的

当我居中我的文本时,它在整个图像比例之后居中(在大屏幕上,你可以看到整个图像,它的居中-在小屏幕上它的偏移量,因为一些图像被裁剪到合适的位置。

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-29 17:41:22

您需要添加一个额外的视图来包装ImageBackground,并将样式属性justifyContent和alignItems添加到中心位置,我认为这会消除您的问题。

代码语言:javascript
复制
<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>
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56357246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档