在React Native中,Flex表现出色,实现了一些令人惊叹的功能。

huangapple go评论59阅读模式
英文:

Flex doing crazy things in React Native

问题

Flex让我的应用看起来很混乱。

import {
  StyleSheet,
  Text,
  View,
  Button,
  Pressable,
  Image,
  ScrollView,
} from "react-native";

export default function App() {
  return (
    <View style={styles.container}>
      <ScrollView contentContainerStyle={styles.contentContainer}>
        <ScrollView
          horizontal={true}
          contentContainerStyle={styles.horizContainer}
        >
          <Image
            style={styles.bookWireframe}
            source={require("./assets/images/bookWireframe.png")}
            resizeMode="contain"
          />
          <Image
            style={styles.bookWireframe}
            source={require("./assets/images/bookWireframe2.png")}
            resizeMode="contain"
          />
          <Image
            style={styles.bookWireframe}
            source={require("./assets/images/bookWireframe2.png")}
            resizeMode="contain"
          />
        </ScrollView>
        <View style={styles.buttonContainer}>
          <Pressable style={styles.purpleButton}></Pressable>
        </View>
        <View style={styles.textContainer}>
          <Text style={styles.text}>DUE 01/24</Text>
        </View>
      </ScrollView>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    height: 700,
    width: "100%",
  },
  contentContainer: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    color: "green",
    backgroundColor: "aquamarine",
    flexDirection: "column",
  },
  horizContainer: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
  },
  bookWireframe: { width: "20%" },
  buttonContainer: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    width: "100%",
    backgroundColor: "red",
  },
  purpleButton: {
    backgroundColor: "purple",
    width: "80%",
    paddingVertical: 30,
    borderWidth: 4,
    borderColor: "black",
    borderRadius: "50%",
  },
  textContainer: {
    flex: 1,
  },
  text: {
    fontSize: 40,
    fontWeight: "700",
    fontStyle: "italic",
  },
});

这里是Expo Go页面的图像。

思路是有三个容器,horizContainer、buttonContainer和textContainer,它们嵌套在contentContainer内(垂直的ScrollView内)。如果父级ScrollView的flex为1,子View和ScrollViews都有flex为1,它们不应该均匀分布在父级内吗?子水平ScrollView内还有图像。

由于某种原因,水平ScrollView内的填充很大,无法通过{{padding: 0}}关闭。而且对我来说,弹性盒布局不起作用。

ScrollView无法向下滚动并保持在那里。不过,水平滚动条之前可以正常工作,当宽度大于屏幕宽度并且图像足够大时,可以滚动并保持在那里。

我试图按照 StackOverflow 帖子中的答案/容器建议进行操作,但并没有完全解决问题。他们的建议是将最外层的ScrollView包装在一个View中,为其提供更静态的滚动高度。

我原本期望有三个垂直堆叠的列,可以向下滚动的块。但得到的是一个极其混乱、不可滚动的版本。

英文:

Flex is making my app look like a disaster.

import {
  StyleSheet,
  Text,
  View,
  Button,
  Pressable,
  Image,
  ScrollView,
} from &quot;react-native&quot;;

export default function App() {
  return (
    &lt;View style={styles.container}&gt;
      &lt;ScrollView contentContainerStyle={styles.contentContainer}&gt;
        &lt;ScrollView
          horizontal={true}
          contentContainerStyle={styles.horizContainer}
        &gt;
          &lt;Image
            style={styles.bookWireframe}
            source={require(&quot;./assets/images/bookWireframe.png&quot;)}
            resizeMode=&quot;contain&quot;
          /&gt;
          &lt;Image
            style={styles.bookWireframe}
            source={require(&quot;./assets/images/bookWireframe2.png&quot;)}
            resizeMode=&quot;contain&quot;
          /&gt;
          &lt;Image
            style={styles.bookWireframe}
            source={require(&quot;./assets/images/bookWireframe2.png&quot;)}
            resizeMode=&quot;contain&quot;
          /&gt;
        &lt;/ScrollView&gt;
        &lt;View style={styles.buttonContainer}&gt;
          &lt;Pressable style={styles.purpleButton}&gt;&lt;/Pressable&gt;
        &lt;/View&gt;
        &lt;View style={styles.textContainer}&gt;
          &lt;Text style={styles.text}&gt;DUE 01/24&lt;/Text&gt;
        &lt;/View&gt;
      &lt;/ScrollView&gt;
    &lt;/View&gt;
  );
}

const styles = StyleSheet.create({
  container: {
    height: 700,
    width: &quot;100%&quot;,
  },
  contentContainer: {
    flex: 1,
    justifyContent: &quot;center&quot;,
    alignItems: &quot;center&quot;,
    color: &quot;green&quot;,
    backgroundColor: &quot;aquamarine&quot;,
    flexDirection: &quot;column&quot;,
  },
  horizContainer: {
    flex: 1,
    justifyContent: &quot;center&quot;,
    alignItems: &quot;center&quot;,
  },
  bookWireframe: { width: &quot;20%&quot; },
  buttonContainer: {
    flex: 1,
    justifyContent: &quot;center&quot;,
    alignItems: &quot;center&quot;,
    width: &quot;100%&quot;,
    backgroundColor: &quot;red&quot;,
  },
  purpleButton: {
    backgroundColor: &quot;purple&quot;,
    width: &quot;80%&quot;,
    paddingVertical: 30,
    borderWidth: 4,
    borderColor: &quot;black&quot;,
    borderRadius: &quot;50%&quot;,
  },
  textContainer: {
    flex: 1,
  },
  text: {
    fontSize: 40,
    fontWeight: &quot;700&quot;,
    fontStyle: &quot;italic&quot;,
  },
});

Here's an image of the Expo Go page.

The idea is that there are three containers, horizContainer, buttonContainer, and textContainer, and that they are nested inside contentContainer (the vertical ScrollView). If the parent ScrollView has flex of 1, and the children Views and ScrollViews all have flex of 1, shouldn't they be evenly split within the parent? There's also images inside the child horizontal ScrollView.

For some reason the padding inside the horizontal ScrollView is massive, and can't be turned off with {{padding: 0}}. And the flexbox just doesn't work for me.

ScrollView won't scroll down and stay there, either. The horizontal one was working earlier though, and scrolls and stays when the width is wider than the screen-width and the images are big enough.

I was trying to follow the answer / container advice from this StackOverflow thread, but it hasn't completely solved things. Their advice is to wrap the outermost ScrollView in a View, that gives it a more static height to scroll within.

I was expecting three stacked vertical columns, blocks that could scroll downwards. Got an extremely mangled, non-scrollable version of that.

答案1

得分: 0

-编辑-
在仔细查看代码后,我认为你实际上需要提示包含图像的视图其高度,可以使用 flexBasisheight

我认为你的三个容器缺少 flexShrink: 1 和可能是 flexGrow: 1。这里有一个小的示例,可以看到这些值如何影响布局:https://snack.expo.dev/@viljarremato/flex-basis,-grow,-and-shrink

React Native中,flexShrink 默认为0,而不是CSS中的1,这是一个常见的陷阱,我自己偶尔也会掉进去。

英文:

-edit-
After looking more into the code, I think you actually need to hint the view containing image about it's height, either using flexBasis or height.

I believe you are missing flexShrink: 1 and maybe flexGrow: 1 on your 3 containers. Here is a small playground to see how the values affect the layout: https://snack.expo.dev/@viljarremato/flex-basis,-grow,-and-shrink

In React Native flexShrink is defaulting to 0 instead of 1 in CSS, it is common pitfall I occasionaly fall into myself.

huangapple
  • 本文由 发表于 2023年3月9日 22:33:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定