【问题标题】:How to align image above another using absolute position on different devices如何在不同设备上使用绝对位置将图像对齐到另一个上方
【发布时间】:2020-12-09 23:59:35
【问题描述】:

当背景图像可以通过不同的设备分辨率调整大小时,是否可以使用绝对位置对齐图像并保持相同的位置? 还有其他解决办法吗?

这是我目前正在尝试解决的问题:

414 x 896 像素设备上的图像

360 x 672px 设备上的图像

这就是我想要做的:

    <View style={{position: 'relative'}}>
      <Image
        resizeMode="contain"
        style={{flex: 1, flexGrow: 1}}
        source={Assets.Mobile.RemoteControl} // The background image with 3 icons
      />
      <Image
        style={{
          position: 'absolute',
          top: '6.8%',
          left: '19%',
        }}
        source={Assets.Mobile.RemoteRectangleButtonBorder}  // The rounded border
      />
    </View>

【问题讨论】:

    标签: react-native stylesheet


    【解决方案1】:

    使用 flexDirection: 'row' 为按钮创建一个容器,然后将按钮创建为单个按钮而不是一张图片

    // Container
    <View style={{flexDirection: 'row', justifyContent: 'space-evenly'}}>
    
      // Button 1
      <View style={{justifyContent: 'center', alignContent: 'center'}}>
      // Big ring
      <Image source={{uri or require: "url1"}} style={{position: 'absolute', height: xy%, width: ab%}}/>
      // Smal Ring
      <Image source={{uri or require: "url1"}} style={{position: 'absolute'}}/>
      </View>
    
      // Button 2
      <View>
      // House
      <Image source={{uri or require: "house"}} style={{position: 'absolute', height: xy%, width: ab%}}/>
      </View>
    
      // Button 3
      <View>
      // List
      <Image source={{uri or require: "list"}} style={{position: 'absolute', height: xy%, width: ab%}}/>
      </View>
    
    </View>
    

    分割成单独的视图使我们能够在嵌套的较小视图中居中放置图像。

    【讨论】:

    • 感谢@Erik Dreifaldt。我已经尝试过类似于这个的解决方案,但它的问题是设计师不希望屏幕在小型设备上滚动,并且由于我有很多按钮,所以当我这样做时很难避免这种情况不要使用图像来调整它的大小
    • 为什么需要滚动?
    • 因为它超过560px的高度,而且很容易将图像调整为340px,而不是多个按钮和资产。
    • 调整多个按钮和资源的大小相当简单。看看 useWindowDimensions。 reactnative.dev/docs/usewindowdimensions
    猜你喜欢
    • 2020-12-24
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    相关资源
    最近更新 更多