繁体   English   中英

为什么 react-native 中的默认视图宽度为 100%? 为什么使用 alignItems 时没有默认宽度?

[英]Why is default view width is 100% in react-native? and why there is no default width when using alignItems?

我现在在 react-native 布局没有问题,只是想知道默认宽度概念的原因

下面这个脚本几乎是一样的

<View
   style = {{
      flex: 1
   }}
>
   //The view below has default width "100%"
   <View
      style = {{
          backgroundColor: "red",
          height: 60
      }}
   />
</View>
<View
   style = {{
      alignItems: "flex-start", //Same render from left, using alignItems makes inside content having no default width 
      flex: 1
   }}
>
   //The view below has no width
   <View
      style = {{
          backgroundColor: "red",
          height: 60
      }}
   />
</View>

它应该具有相同的宽度行为吗?

如果View中的div类比

  • 默认是display=block
  • display=block默认为width=100%
  • 当您使用alignItems时,它不再是display=block ,而是display=flex ,因此宽度不是 100%

  • 要使用alignItems使width=100% ,您必须使用alignItems='stretch' 或者给孩子flex=1

虽然View没有display=block属性,但它只是默认行为

所以问题不在于 react-native,问题在于您对 flexbox 的理解

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM