我最终将本机版本恢复为0.42,其中包括引入flexGrow
,flexShrink
和flexBasis
以及如何呈现flex
的更改(或修复)。
我一直收到如下错误:
使用显式设置的宽度/高度渲染视图,但使用0 flexBasis。 (这可以通过改变flex来修复:到flexGrow :)查看:
有人可以解释flex: 1
与flexGrow: 1
之间的区别。如果我将一个或另一个应用于一个视图,它似乎做了不同的事情,但它不应该做同样的事情吗?
答案 0 :(得分:131)
这里有一些需要考虑的测试代码:
render() {
return <View style={{flex: 1,backgroundColor: "cornflowerblue"}}>
<View style={{backgroundColor: "chartreuse"}}><Text>Nothing (17px)</Text></View>
<View style={{flex: 0, backgroundColor: "yellow"}}><Text>flex: 0 (17px)</Text></View>
<View style={{flex: 0, flexBasis: 10, backgroundColor: "brown"}}><Text>flex: 0, flexBasis: 10 (10px)</Text></View>
<View style={{flex: 0, flexGrow: 1, backgroundColor: "orange"}}><Text>flex: 0, flexGrow: 1 (97px)</Text></View>
<View style={{flex: 0, flexShrink: 1, backgroundColor: "tan"}}><Text>flex: 0, flexShrink: 1 (17px)</Text></View>
<View style={{flex: 0, flexGrow: 1, flexBasis: 10, backgroundColor: "purple"}}><Text>flex: 0, flexGrow: 1, flexBasis: 10 (90px)</Text></View>
<View style={{flex: 0, flexShrink: 1, flexBasis: 10, backgroundColor: "gray"}}><Text>flex: 0, flexShrink: 1, flexBasis: 10 (10px with 7px hidden below the next element)</Text></View>
<View style={{flex: 1, backgroundColor: "blue"}}><Text>flex: 1 (80px)</Text></View>
<View style={{flex: 1, flexBasis: 10, backgroundColor: "cornsilk"}}><Text>flex: 1, flexBasis: 10 (90px)</Text></View>
<View style={{flex: 1, flexGrow: 1, backgroundColor: "red"}}><Text>flex: 1, flexGrow: 1 (80px)</Text></View>
<View style={{flex: 1, flexShrink: 1, backgroundColor: "green"}}><Text>flex: 1, flexShrink: 1 (80px)</Text></View>
<View style={{flex: 1, flexGrow: 1, flexBasis: 10, backgroundColor: "aqua"}}><Text>flex: 1, flexGrow: 1, flexBasis: 10 (90px)</Text></View>
<View style={{flex: 1, flexShrink: 1, flexBasis: 10, backgroundColor: "pink"}}><Text>flex: 1, flexShrink: 1, flexBasis: 10 (90px)</Text></View>
</View>;
}
这是以上代码的屏幕截图:
添加了width
和height
:
render() {
return <View style={{flex: 1,backgroundColor: "cornflowerblue"}}>
<View style={{flex: 0, backgroundColor: "orange"}}><Text>flex: 0 (17px)</Text></View>
<View style={{flex: 0, width: 700, height: 20, backgroundColor: "yellow"}}><Text>flex: 0, width: 700, height: 20 (20px)</Text></View>
<View style={{flex: 0, flexBasis: 10, width: 700, height: 20, backgroundColor: "brown"}}><Text>flex: 0, flexBasis: 10, width: 700, height: 20 (10px with 7px hidden below the next element)</Text></View>
<View style={{flex: 0, flexGrow: 1, width: 700, height: 20, backgroundColor: "orange"}}><Text>flex: 0, flexGrow: 1, width: 700, height: 20 (90px)</Text></View>
<View style={{flex: 0, flexShrink: 1, width: 700, height: 20, backgroundColor: "tan"}}><Text>flex: 0, flexShrink: 1, width: 700, height: 20 (20px)</Text></View>
<View style={{flex: 0, flexGrow: 1, flexBasis: 10, width: 700, height: 20, backgroundColor: "purple"}}><Text>flex: 0, flexGrow: 1, flexBasis: 10, width: 700, height: 20 (80px)</Text></View>
<View style={{flex: 0, flexShrink: 1, flexBasis: 10, width: 700, height: 20, backgroundColor: "gray"}}><Text>flex: 0, flexShrink: 1, flexBasis: 10, width: 700, height: 20 (10px with 7px hidden below the next element)</Text></View>
<View style={{flex: 1, backgroundColor: "orange"}}><Text>flex: 1 (70px)</Text></View>
<View style={{flex: 1, width: 700, height: 20, backgroundColor: "blue"}}><Text>flex: 1, width: 700, height: 20 (70px)</Text></View>
<View style={{flex: 1, flexBasis: 10, width: 700, height: 20, backgroundColor: "cornsilk"}}><Text>flex: 1, flexBasis: 10, width: 700, height: 20 (80px)</Text></View>
<View style={{flex: 1, flexGrow: 1, width: 700, height: 20, backgroundColor: "red"}}><Text>flex: 1, flexGrow: 1, width: 700, height: 20 (70px)</Text></View>
<View style={{flex: 1, flexShrink: 1, width: 700, height: 20, backgroundColor: "green"}}><Text>flex: 1, flexShrink: 1, width: 700, height: 20 (70px)</Text></View>
<View style={{flex: 1, flexGrow: 1, flexBasis: 10, width: 700, height: 20, backgroundColor: "aqua"}}><Text>flex: 1, flexGrow: 1, flexBasis: 10, width: 700, height: 20 (80px)</Text></View>
<View style={{flex: 1, flexShrink: 1, flexBasis: 10, width: 700, height: 20, backgroundColor: "pink"}}><Text>flex: 1, flexShrink: 1, flexBasis: 10, width: 700, height: 20 (80px)</Text></View>
</View>;
}
这是以上代码的屏幕截图:
flex: 0
(默认)flex: 0
强>
width
和height
道具来调整大小,但如果没有设置,它似乎适合内容。flex: 0, flexBasis: {{px}}
强>
flexBasis
flex: 0, flexGrow: 1
强>
flex: 0
和flexGrow: 1
;它与将内容的大小(在上面的示例中为a)添加到设置为flex: 1
的元素的大小相同。它与flex: 1, flexBasis: 10
类似,但不是添加大量像素,而是添加内容的大小。flex: 0, flexShrink: 1
强>
flex: 0
和flexShrink: 1
时,元素似乎占用内容的大小,换句话说,它与flex: 0
相同。我敢打赌,有些情况会比内容更大但我还没有看到。flex: 0, flexGrow: 1, flexBasis: {{px}}
强>
flex: 0, flexGrow: 1
相同,但不是将内容大小添加到flex: 1
元素,而是添加给定的像素数。flex: 0, flexShrink: 1, flexBasis: {{px}}
强>
flex: 0, flexBasis: {{px}}
相同。flex: 0, height: {{px}}
强>
flex: 0
时,height
的处理方式与flexBasis
相同。如果同时设置了height
和flexBasis
,则会忽略height
。flex: 1
flex: 1
强>
flex: 1, flexBasis: {{px}}
强>
flex: 1
和flexBasis: {{px}}
; flexBasis
的值将添加到元素的大小中。换句话说,它就像获取flex: 1
元素并添加flexBasis
设置的像素数一样。因此,如果flex: 1
元素为50px,并且您添加flexBasis: 20
,则该元素现在将为70px。flex: 1, flexGrow: 1
强>
flex: 1, flexShrink: 1
强>
flex: 1, flexGrow: 1, flexBasis: {{px}}
强>
flex: 1, flexBasis: {{px}}
相同,因为flexGrow
会被忽略。flex: 1, flexShrink: 1, flexBasis: {{px}}
强>
flex: 1, flexBasis: {{px}}
相同,因为flexShrink
会被忽略。flex: 1, height: {{px}}
强>
flex: 1
,height
将被忽略。请改用flexBasis
。flex: 1
,如果没有它,则所有孩子都不会按照您的预期显示。Hot Reloading
,它会在重新加载几次后错误地显示元素。我建议启用Live Reload
或使用命令 + r (很多)。 flex: 0
。如果您没有添加flex样式值,则默认为0。width
始终与flexDirection: "column"
一起考虑。这同样适用于height
flexDirection: "row"
。flexBasis
使用height
,因为flexBasis
胜过height
。