保持flex属性和固定高度,同时在React Native中为多个项目启用滚动

zbdgwd5y  于 6个月前  发布在  React
关注(0)|答案(1)|浏览(42)

我在开发react-native方面非常天真,请帮助我,首先我使用flex属性编写所有代码,现在我想对该项目进行多次渲染,所以当项目渲染4次时,它会正常,因为屏幕上有4个元素的空间,但是当我尝试引入第5个元素时,因为屏幕只有4个空间,所以第5个元素尝试调整,所以我想让我的Flex属性保持原样例如:我有2个页面,每个页面包含4个项目,然后页面以链格式连接,我有滚动条,这样我的Flex属性就不会受到干扰,我也有滚动条。
在App.js中

function App() {
  return (
    <View style={[styles.container, { backgroundColor: '#e4e3e3' }]}>
      <View style={{ flex: 1 / 5 }} >

        <Header name='Community' subHeading='Mobile App Developer Enthusiasts' />
      </View>
      <View style={{ flex: 4 / 5 }} >
        <CommunitiesList image={require("./assets/cover.png")} />
        <CommunitiesList image={require("./assets/cover.png")} />
        <CommunitiesList image={require("./assets/cover.png")} />
        <CommunitiesList image={require("./assets/cover.png")} />

      </View>
    </View>
  );
}
const styles = StyleSheet.create({
  container: {
    flex: 1
  }
});

字符串
在CommunitesList.js中

import React, { useEffect, useState } from "react";
import { StyleSheet, View, Image, Dimensions, Text } from "react-native";

const CommunitiesList = (props) => {
    const { image } = props
    return (
        <View style={{ flex: 1 / 4, flexDirection: 'row', justifyContent: 'flex-end', }}>
            <View style={{ flex: 1, justifyContent: 'center', }}>
                <Image
                    style={styles.cover}
                    resizeMode="cover"
                    source={image}
                />
            </View>
            <View style={[styles.upperStyle, { marginTop: 7, backgroundColor: '#373eb2', height: 30, paddingRight: 6, paddingLeft: 6, position: 'absolute', justifyContent: 'center' }]}>
                <Text style={[{ color: 'white', padding: 2 }, styles.upperHeading]}>Data Science Enthusiasts</Text>
            </View>
        </View>

    );
};
const styles = StyleSheet.create({
    cover: {
        height: 132,
        width: '100%',
        position: "absolute",
    },
    upperHeading: {
        fontSize: 11,
        fontWeight: "700",
        fontFamily: "Calibri",

    },
    upperStyle: {
        borderBottomRightRadius: 12,
        borderBottomLeftRadius: 12,
        backgroundColor: "#373eb2",
    }
});

export default CommunitiesList;


有没有人能帮帮忙,这样CommunityList就可以被渲染多次,而不会像我在4中那样损害Flex属性,只是在它们上面有一个滚动条

rryofs0p

rryofs0p1#

Flex基于父容器的大小,所以这不起作用-在这种情况下,它总是试图将所有内容都放入屏幕中。您必须手动计算尺寸:

const { width: windowWidth } = Dimensions.get('window');
const itemWidth = windowWidth / 4;

字符串
如果你需要根据父视图的大小而不是整个窗口来计算,那么可以在父视图上使用onLayout属性来获取它的大小。
您还必须将组件嵌入到水平ScrollView中:

<ScrollView
  horizontal
  //...
>
  <Component />
  <Component />
  {/* ... */}
</ScrollView>

相关问题