如何在React-native中的ScrollView内使用柔性 Package

qeeaahzv  于 2022-12-04  发布在  React
关注(0)|答案(3)|浏览(134)

我无法在ScrollView容器内使用flexWrap属性。
我的代码如下所示

function Home({ navigation }) {
  return (
    <View style={styles.container}>
      <ScrollView style={styles.scrollContainer}>
        <LayoutButton
          navigation={navigation}
          title={"go to setting"}
          path="Setting"
        />
        <LayoutButton
          navigation={navigation}
          title={"go to setting"}
          path="Setting"
        />
        <LayoutButton
          navigation={navigation}
          title={"go to setting"}
          path="Setting"
        />
        <LayoutButton
          navigation={navigation}
          title={"go to setting"}
          path="Setting"
        />
        
      </ScrollView>
      <StatusBar style="auto" />
    </View>
  );
}

和风格的

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center",
    flexWrap: "wrap",
  },
  scrollContainer: {
    backgroundColor: "#B7C3F3",
    width: "100%",
    flexWrap: "wrap",
  },
});

我不知道Flex是否在ScrollView下工作。我是新来的本地人,请帮助我解决这个问题,并提前感谢

hfsqlsce

hfsqlsce1#

React Native中的ScrollView组件不支持flexWrap属性。这是因为ScrollView组件设计为水平或垂直滚动内容,具体取决于水平和垂直属性的配置。它不支持在其容器中换行内容。
如果要使用flexWrap属性,可以使用支持换行得其他布局组件,如View组件.可以将View组件嵌套在ScrollView组件中,然后将flexWrap属性应用于嵌套得View组件,如下所示:

function Home({ navigation }) {
  return (
    <View style={styles.container}>
      <ScrollView style={styles.scrollContainer}>
        <View style={styles.wrapContainer}>
          <LayoutButton
            navigation={navigation}
            title={"go to setting"}
            path="Setting"
          />
          <LayoutButton
            navigation={navigation}
            title={"go to setting"}
            path="Setting"
          />
          <LayoutButton
            navigation={navigation}
            title={"go to setting"}
            path="Setting"
          />
          <LayoutButton
            navigation={navigation}
            title={"go to setting"}
            path="Setting"
          />
        </View>
      </ScrollView>
      <StatusBar style="auto" />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center",
  },
  scrollContainer: {
    backgroundColor: "#B7C3F3",
    width: "100%",
  },
  wrapContainer: {
    flexWrap: "wrap",
  },
});

在此示例中,具有wrapContainer样式的View组件嵌套在ScrollView组件中,并且将flexWrap属性应用于View组件。这允许View组件的子元素在容器中换行。
请注意,您可能需要调整View组件及其子元素得width与flex属性,以确保它们在容器中正确换行.您可能还需要调整ScrollView组件得width与height属性,以确保它足够大,可以包含所有换行得内容.

qxsslcnc

qxsslcnc2#

要在React Native中的ScrollView容器内使用flexWrap属性,需要确保ScrollView容器具有固定高度。这是因为ScrollView容器的默认高度为“auto”,这不允许flexWrap属性工作。
若要解决此问题,可以向ScrollView容器的样式添加height属性,并将其设置为固定值。例如:

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
flexWrap: "wrap",
},
scrollContainer: {
backgroundColor: "#B7C3F3",
width: "100%",
flexWrap: "wrap",
height: "100%", // Add a height property with a fixed value
},
});
zsohkypk

zsohkypk3#

尝试删除flex: 1以解决问题

相关问题