【问题标题】:React native two column layout for dynamic elements not working为动态元素反应原生两列布局不起作用
【发布时间】:2017-09-12 13:13:39
【问题描述】:

内含图片的多个View元素需要以两列布局显示。

我正在使用以下代码(基本上,获取窗口的维度(x)并设置视图宽度 = x/2)。

父容器是flex: row

但这似乎不起作用。我连续只得到一个图像,而不是图像中显示的两个。我做错了什么?

const window = Dimensions.get('window');
const imageWidth = (window.width/3)+30;
const imageHeight = window.width/3;
export default class Brochures extends Component {
  render() {
    const brochuresView = brochuresData.map( Brochure );
    return (
      <ScrollView style={styles.container}>
        {brochuresView}
      </ScrollView>
    );
  }
}

const Brochure = ( data ) => {
  const {child, image} = styles;
  return (
    <View key={data.id} style={child}>
      <Image
        style={image}
        source={{uri: data.url}}
      />
    </View>
  )
}

const styles = StyleSheet.create({
  container: {
    flexDirection: 'row',
    flexWrap: 'wrap'
  },
  child: {
    width: window.width/2,
    alignItems: 'center',
    height: imageHeight+5,
    marginTop: 10
  },
  image: {
    width: imageWidth,
    height: imageHeight
  }
});

【问题讨论】:

    标签: react-native flexbox


    【解决方案1】:

    ScrollView 采用 contentContainerStyle 属性,而普通的 style 属性将描述样式。

    来自文档:

    这些样式将应用于滚动视图内容容器 它包装了所有子视图。示例:

    return ( <ScrollView contentContainerStyle={styles.contentContainer}> </ScrollView> ); ... const styles = StyleSheet.create({ contentContainer: { paddingVertical: 20 } });

    所以基本上您需要对代码进行的唯一更改是:

    <ScrollView contentContainerStyle={styles.container}>
    

    对比这个:

    <ScrollView style={styles.container}>
    

    【讨论】:

      【解决方案2】:

      试试这个:

      const window = Dimensions.get('window');
      const imageWidth = (window.width/3)+30;
      const imageHeight = window.width/3;
      export default class Brochures extends Component {
        render() {
          const brochuresView = brochuresData;
          var brochuresRow = [];
          var set = [];
          var setCounter = 0;
      
          for(var i = 0; i < brochuresView.length; i++) {
              set.push(brochuresView[i]);
              if((i + 1) % 2 == 0 || (i + 1) >= brochuresView.length) {
                  setCounter++;
                  brochuresRow.push(set);
                  set = [];
              }
          }
      
          return (
            <ScrollView>
              {brochuresRow.map((row, i) => (
                  <View key={i} style={styles.container}>
                      {row.map((brochure, ii) => (
                          <View key={ii} style={styles.child}>
                              <Image
                                  style={styles.image}
                                  source={{uri: brochure.url}}
                              />
                          </View>
                      ))}
                  </View>
              ))}
            </ScrollView>
          );
        }
      }
      
      const styles = StyleSheet.create({
        container: {
          flexDirection: 'row',
          flex: 1,
          justifyContent: 'center'
        },
        child: {
          height: imageHeight+5,
          marginTop: 10
        },
        image: {
          width: imageWidth,
          height: imageHeight
        }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-03
        • 2018-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-02
        • 1970-01-01
        相关资源
        最近更新 更多