【问题标题】:React Native <Text> overflows <View> when in a flexReact Native <Text> 在 flex 中溢出 <View>
【发布时间】:2017-12-30 14:52:30
【问题描述】:

所以对于上面的图像,我试图让“绿色”框环绕动态文本。请注意蓝色和黄色文本框是如何在 flex: 'row' 配置中的,蓝色文本框是 flex: 2 而黄色是 flex: 1

在文本对于父容器来说太大之前一切正常。

我希望绿色容器根据需要增长以适应弯曲的内在孩子。这可能与本机反应吗?

这是网络意义上的图像:

我尝试将绿色框设置为flex: 1,但它太大了,因为它填满了整个屏幕。设置height 是可能的,但我不知道最大的内部组件的大小(至少没有一些hackery)。我什至试过minHeight

有没有人尝试过这样的事情?我需要自己动态获取内部元素的高度吗?

更新 - 这是一段代码:

  <View style={{ flex: 1, flexDirection: 'column', marginTop: 70, backgroundColor: '#f9e2ff' }}>
    <View
      style={{
        minHeight: 40,
        borderWidth: 1,
        borderStyle: 'solid',
        padding: 5,
        margin: 5,
        backgroundColor: '#58c09e'
      }}
    >
      <View style={{ flex: 1, flexDirection: 'row' }}>
        <View style={{ flex: 2, backgroundColor: '#eeff96' }}>
          <Text>
            Hello this is a long bit of text that will fill up the entire column to see how the text will wrap
          </Text>
        </View>
        <View style={{ flex: 1, backgroundColor: '#eeffff' }}>
          <Text>Hello again?</Text>
        </View>
      </View>
    </View>
  </View>

感谢收看。

【问题讨论】:

  • 你能包含一些代码吗?
  • 如果您设置高度,那么该高度不会改变,您要么必须使用弹性比率,要么设置最小高度/最大高度并对文本执行相同操作,以便它们一起缩放和文本不会超出视野
  • @Chris 请添加代码以便我们提供帮助:)
  • 感谢查看,已添加代码。

标签: react-native flexbox react-native-flexbox


【解决方案1】:

好吧,在环顾四周并阅读:flex vs flexGrow vs flexShrink vs flexBasis in React Native? 我能够找出问题所在。

我需要的是行上的flex: 0

  <View style={{ flex: 1, flexDirection: 'column', marginTop: 70, backgroundColor: '#f9e2ff' }}>
    <View
      style={{
        minHeight: 40,
        borderWidth: 1,
        borderStyle: 'solid',
        padding: 5,
        margin: 5,
        backgroundColor: '#58c09e'
      }}
    >
      <View style={{ flex: 0, flexDirection: 'row' }}>
        <View style={{ flex: 2, backgroundColor: '#eeff96' }}>
          <Text>
            Hello this is a long bit of text that will fill up the entire column to see how the text will wrap
          </Text>
        </View>
        <View style={{ flex: 1, backgroundColor: '#eeffff' }}>
          <Text>Hello again?</Text>
        </View>
      </View>
      <View>
        <Text>Here's another test</Text>
      </View>
    </View>
  </View>

这会产生我所期望的:

所以我猜 flexbox 不像他们在文档中所说的那样“在 React Native 中的工作方式与在 Web 上的 CSS 中的工作方式相同”。

【讨论】:

  • 我也遇到了同样的问题,确认这也适用于我。非常感谢!
【解决方案2】:

试试这个代码 -->

<View style={{flex:1,flexDirection:'row',borderWidth:1,padding:5}}>
  <View style={{flex:0.7}}>
      <Text>
            Hello This is long bit of text that will fill up the entire 
            column to see how the text will wrap
      </Text>
  </View>

  <View style={{flex:0.3}}>
      <Text>Hello Again</Text>
  </View>

</View>

【讨论】:

  • 你是说内部的“flex”应该加到父flex吗?而不是我对行中每个元素的加权flex: 2flex: 1
  • 我尝试使用0.70.3 flex 尺寸,结果相同(参见上面的代码)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-09
  • 1970-01-01
  • 2016-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多