代码之家  ›  专栏  ›  技术社区  ›  Arkon

自动调整N个图像的大小以适应容器

  •  0
  • Arkon  · 技术社区  · 7 年前

    我有以下设计:

    <View style={{flexDirection: "column", justifyContent: "space-around", alignItems: "center", flex: 1}}>
      <TopElement style={{position: "absolute", top:5}}/>
      <Image key={1}/>
      <Image key={2}/>
      ...
      <Image key={n}/>
      <BottomElement style={{position: "absolute", bottom:5}}/>
    </View>
    

    我的 n 图像太大,无法容纳包装容器(例如我的屏幕)。是否可以在不接触的情况下调整尺寸(保持纵横比) TopElement BottomElement

    我不确定是否有内置功能,是否使用布局FlexBox道具是解决方案,或者可能根据屏幕高度手动计算每个图像的尺寸,或者可能添加另一个视图来包装它们。。。?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Val    7 年前

    您可以按以下方式操作:

    <View style={{flexDirection: "column", justifyContent: "space-around",
          alignItems: "center", flex: 1, paddingTop: 30, paddingBottom: 30}}>
      <TopElement style={{position: "absolute", top:5}}/>
      <Image resizeMode="contain" style={{flex: 1}} key={1}/>
      ...
      <Image resizeMode="contain" style={{flex: 1}} key={n}/>
      <BottomElement style={{position: "absolute", bottom:5}}/>
    </View>
    
    1. 使用 paddingTop & paddingBottom 在父项上 <View /> ,高度为 <TopElement /> <BottomElement /> . 使顶部/底部不会与图像重叠。

    2. 设置 <Image /> 道具 resizeMode 'contain' . 使图像在保持原始纵横比的情况下自动调整大小。