【问题标题】:Flexbox height in React NativeReact Native 中的 Flexbox 高度
【发布时间】:2018-08-15 03:01:58
【问题描述】:

我正在尝试在 React Native 中创建一个简单的内容大小的对话框。在这个层次结构中最高的我有我的覆盖<View> 样式如下:

 {
   position: 'absolute',
   top: 0,
   left: 0,
   width: '100%',
   height: '100%',
   zIndex: 100,
   backgroundColor: COLOR
} 

在这里面,我有一个包装器<View> 具有这种风格:

{
   flex: 1,
   alignItems: 'center',
   justifyContent: 'center'
}

然后我有对话框<View>

{
   flex: 1,
   width: '86%',
   maxWidth: 450,
   flexDirection: 'column',
   justifyContent: 'flex-start',
   alignItems: 'flex-start',
   backgroundColor: 'white',
   borderWidth: 5,
   borderColor: 'gold'
}

不过,问题似乎在于我构建对话框的方式:

<View style={{flex: 1, flexDirection: 'row', justifyContent: 'flex-start', alignItems: 'flex-start'}}>
  <Image source={require('../../assets/icons/warning.png')} 
   style={{resizeMode: 'contain', height: 50, width: 48, marginRight: 30}} />
  <View style={[flex: 1, flexDirection: 'column', justifyContent: 'flex-start', alignItems: 'flex-start']}>
    <Text>{this.props.dialogMessage}</Text>
    <View style={{flex: 1, width: '100%', flexDirection: 'row', justifyContent: 'space-between', alignItems: 'flex-start'}}>
       <Button dismissOverlay={this.props.dismissOverlay} />
    </View>
  </View>
</View>

通过这种样式,我得到了这样的结果:

如果我将 flex 更改为 0,我会改为:

如何让对话框根据内容调整大小?

【问题讨论】:

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


【解决方案1】:

在你的对话框&lt;View&gt; 中根本不要设置flex。就这样吧

{
   width: '86%',
   maxWidth: 450,
   flexDirection: 'column',
   justifyContent: 'flex-start',
   alignItems: 'flex-start',
   backgroundColor: 'white',
   borderWidth: 5,
   borderColor: 'gold'
}

如果它不起作用,您能否提供更多关于 Dialog view 内内容的代码?

【讨论】:

  • 我认为您怀疑问题出在对话框中的视图是正确的。如果我用简单的&lt;Text&gt; 运行替换它们,对话框的大小会正确。我将编辑我的问题以包含内部观点。
  • 我将您的解决方案应用于对话框子视图,删除了 flex 属性,并且成功了!谢谢!
  • 关键是从顶部 &lt;View&gt; 和直接子 &lt;View&gt; 中删除 flex 属性
猜你喜欢
  • 1970-01-01
  • 2016-01-22
  • 2021-05-30
  • 1970-01-01
  • 1970-01-01
  • 2016-01-30
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多