【问题标题】:React Native Styling inside ImageBackground在 ImageBackground 中反应原生样式
【发布时间】:2018-12-24 21:22:48
【问题描述】:

我正在构建一个制作邀请卡的应用程序,我正在使用 ImageBackground 制作卡片然后设置里面的内容,但是我遇到了不同的屏幕尺寸和分辨率的问题,我的代码如下:

<ImageBackground
          resizeMode='contain'
          backgroundColor={this.state.bgColor}
          style={{
               height: height / 3 + 20,
               width: width,}}
               source={this.state.border}>

       <View style={{ flex: 1, marginRight: 10,
                      marginLeft: 45, marginTop: 10 }}>

             <Text style={{ fontSize: height / 37, 
                            color: this.state.fontColor, 
                            fontWeight: '700',
                            textAlign: 'center', 
                            fontFamily: this.fonttext }}>{this.type}</Text>

              <Text style={{ marginLeft: 40,
                            marginRight: 5, marginTop: 10, 
                            fontSize: height / 48, 
                            color: this.state.fontColor, 
                            textAlign: 'right', 
                            fontFamily: this.fonttext }}>{this.body}</Text>

             <Text style={{ fontSize: height / 49, 
                            marginTop: 10, 
                            color: this.state.fontColor, 
                            textAlign: 'right', 
                            fontFamily: this.fonttext }}>{this.location}</Text>


             <View style={{ alignItems: 'center', 
                            justifyContent: 'center', 
                            marginTop: (height / 3 + 10) / 11 }}>

                  <Text style={{ fontSize: height / 49, 
                                 fontWeight: 'bold', 
                                 color: this.state.fontColor, 
                                 textAlign: 'right', 
                                 fontFamily: this.fonttext }}>From</Text>

                  <Text style={{ fontSize: height / 49, 
                                 color: this.state.fontColor, 
                                 textAlign: 'right', 
                                 fontFamily: this.fonttext }}>{this.fromD}</Text>
                            </View>

             <Text style={{ fontSize: height / 55, 
                            color: this.state.fontColor, 
                            textAlign: 'right', 
                            fontFamily: this.fonttext }}>{'*' + this.notes}</Text>
           </View>



                    </ImageBackground>

在 Iphone 6 上运行良好

但在 Iphone x、8 plus 和 android 设备上,视图发生了变化,背景图像从上方和左侧都有一些边距,尽管我使用了尺寸,所以我可以获得屏幕的高度和宽度,所以我可以相应地设置视图,但不起作用,那么在这种情况下最好的做法是什么,以便所有设备上的视图都相同。

【问题讨论】:

    标签: react-native styles cross-platform imagebackground


    【解决方案1】:

    这个库可以解决这个问题,看看React Native Size Mater

    它有一个非常小的库代码,没有任何依赖,只是对等依赖。

    它有3个功能

    scale(size: number) 将根据您设备的屏幕宽度返回所提供尺寸的线性缩放结果。

    verticalScale(size: number) 将根据您设备的屏幕高度返回所提供尺寸的线性缩放结果。

    moderateScale(size: number, factor?: number)

    有时您不想以线性方式缩放所有内容,这就是适度缩放的用武之地。 很酷的一点是您可以控制调整大小因子(默认为 0.5)。 如果正常比例会使你的尺寸增加+2X,moderateScale只会增加+X,例如:

    祝你好运

    【讨论】:

      【解决方案2】:

      你需要做的就是不要设置图像高度,只需将其宽度设置为屏幕宽度,它就会适应屏幕

      【讨论】:

      • 当我删除高度大小时,imageBackground 消失了
      • 你必须设置 flex:1 和 width:'100%'
      猜你喜欢
      • 2021-06-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      • 2019-03-05
      • 2018-07-27
      • 1970-01-01
      • 2022-01-17
      • 2021-10-19
      相关资源
      最近更新 更多