布局属性
More detailed examples about those properties can be found on the Layout with Flexbox page.
示例
The following example shows how different properties can affect or shape a React Native layout. You can try for example to add or remove squares from the UI while changing the values of the property flexWrap
.
- javascript
- typescript
文档
属性
alignContent
alignContent
controls how rows align in the cross direction, overriding the alignContent
of the parent. 访问 https://developer.mozilla.org/en-US/docs/Web/CSS/align-content 来进一步了解。
Type | 必需 |
---|---|
enum('flex-start', 'flex-end', 'center', 'stretch', 'space-between', 'space-around', 'space-evenly') | 否 |
alignItems
alignItems
aligns children in the cross direction. For example, if children are flowing vertically, alignItems
controls how they align horizontally. It works like align-items
in CSS (default: stretch). See https://developer.mozilla.org/en-US/docs/Web/CSS/align-items for more details.
Type | Required |
---|---|
enum('flex-start', 'flex-end', 'center', 'stretch', 'baseline') | No |
alignSelf
alignSelf
controls how a child aligns in the cross direction, overriding the alignItems
of the parent. It works like align-self
in CSS (default: auto). See https://developer.mozilla.org/en-US/docs/Web/CSS/align-self for more details.
Type | Required |
---|---|
enum('auto', 'flex-start', 'flex-end', 'center', 'stretch', 'baseline') | No |
aspectRatio
Aspect ratio controls the size of the undefined dimension of a node. See https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio for more details.
- On a node with a set width/height, aspect ratio controls the size of the unset dimension
- On a node with a set flex basis, aspect ratio controls the size of the node in the cross axis if unset
- On a node with a measure function, aspect ratio works as though the measure function measures the flex basis
- On a node with flex grow/shrink, aspect ratio controls the size of the node in the cross axis if unset
- Aspect ratio takes min/max dimensions into account
Type | Required |
---|---|
number, string | No |
borderBottomWidth
borderBottomWidth
works like border-bottom-width
in CSS. See https://developer.mozilla.org/en-US/docs/Web/CSS/border-bottom-width for more details.
Type | Required |
---|---|
number | No |
borderEndWidth
When direction is ltr
, borderEndWidth
is equivalent to borderRightWidth
. When direction is rtl
, borderEndWidth
is equivalent to borderLeftWidth
.
Type | Required |
---|---|
number | No |
borderLeftWidth
borderLeftWidth
works like border-left-width
in CSS. See https://developer.mozilla.org/en-US/docs/Web/CSS/border-left-width for more details.
Type | Required |
---|---|
number | No |
borderRightWidth
borderRightWidth
works like border-right-width
in CSS. See https://developer.mozilla.org/en-US/docs/Web/CSS/border-right-width for more details.
Type | Required |
---|---|
number | No |
borderStartWidth
When direction is ltr
, borderStartWidth
is equivalent to borderLeftWidth
. When direction is rtl
, borderStartWidth
is equivalent to borderRightWidth
.
Type | Required |
---|---|
number | No |
borderTopWidth
borderTopWidth
works like border-top-width
in CSS. See https://developer.mozilla.org/en-US/docs/Web/CSS/border-top-width for more details.
Type | Required |
---|---|
number | No |
borderWidth
borderWidth
works like border-width
in CSS. See https://developer.mozilla.org/en-US/docs/Web/CSS/border-width for more details.
Type | Required |
---|---|
number | No |
bottom
bottom
is the number of logical pixels to offset the bottom edge of this component.
It works similarly to bottom
in CSS, but in React Native you must use points or percentages. Ems and other units are not supported.
See https://developer.mozilla.org/en-US/docs/Web/CSS/bottom for more details of how bottom
affects layout.
Type | Required |
---|---|
number, string | No |
columnGap
columnGap
works like column-gap
in CSS. Only pixel units are supported in React Native. See https://developer.mozilla.org/en-US/docs/Web/CSS/column-gap for more details.
Type | Required |
---|---|
number | No |
direction
direction
指定了用户界面的阅读顺序。ltr
表示 left to right,即从左往右阅读。反之rtl
为从右往左阅读。默认值为inherit
,但根节点的值会根据用户所在地的不同而不同。访问 https://yogalayout.com/docs/layout-direction 来进一步了解。
类型 | 必需 | 平台 |
---|---|---|
enum('inherit', 'ltr', 'rtl') | 否 | iOS |
display
display
设置组件的显示类型。可用于元素的显示和隐藏。
它的表现和 CSS 上的display
类似,但目前只支持'flex'和'none'两个值。默认值是'flex'。
类型 | 必需 |
---|---|
enum('none', 'flex') | 否 |
end
当direction
设置为ltr
时,end
等同于right
。当direction
设置为rtl
,end
等同于left
。
此样式的优先级高于left
和right
。
类型 | 必需 |
---|---|
number, string | 否 |
flex
在 React Native 中flex
的表现和 CSS 有些 区别。flex
在 RN 中只能为整数值,其具体表现请参考yoga
布局引擎的文档,其地址为 https://github.com/facebook/yoga
当flex
为一个正整数时,组件尺寸会具有弹性,并根据具体的 flex 值来按比例分配。比如两个组件在同一个父容器中,一个flex
为 2,另一个flex
为 1,则两者的尺寸比为 2:1。 flex: <positive number>
equates to flexGrow: <positive number>, flexShrink: 1, flexBasis: 0
.
当flex
为 0 时,组件尺寸由width
和height
决定,此时不再具有弹性。
当flex
为-1 时,组件尺寸一般还是由width
和height
决定。但是当空间不够时,组件尺寸会收缩到minWidth
和minHeight
所设定的值。
flexGrow
、flexShrink
、flexBasis
和在 CSS 上表现一致。
类型 | 必需 |
---|---|
number | 否 |
flexBasis
flexBasis
is an axis-independent way of providing the default size of an item along the main axis. Setting the flexBasis
of a child is similar to setting the width
of that child if its parent is a container with flexDirection: row
or setting the height
of a child if its parent is a container with flexDirection: column
. The flexBasis
of an item is the default size of that item, the size of the item before any flexGrow
and flexShrink
calculations are performed.
类型 | 必需 |
---|---|
number, string | 否 |
flexDirection
flexDirection
controls which directions children of a container go. row
goes left to right, column
goes top to bottom, and you may be able to guess what the other two do. It works like flex-direction
in CSS, except the default is column
. 访问 https://developer.mozilla.org/en-US/docs/Web/CSS/flex-direction 来进一步了解。
类型 | 必需 |
---|---|
enum('row', 'row-reverse', 'column', 'column-reverse') | 否 |
flexGrow
flexGrow
describes how any space within a container should be distributed among its children along the main axis. After laying out its children, a container will distribute any remaining space according to the flex grow values specified by its children.
flexGrow
accepts any floating point value >= 0, with 0 being the default value. A container will distribute any remaining space among its children weighted by the children’s flexGrow
values.
类型 | 必需 |
---|---|
number | 否 |
flexShrink
flexShrink
describes how to shrink children along the main axis in the case in which the total size of the children overflows the size of the container on the main axis. flexShrink
is very similar to flexGrow
and can be thought of in the same way if any overflowing size is considered to be negative remaining space. These two properties also work well together by allowing children to grow and shrink as needed.
flexShrink
accepts any floating point value >= 0, with 0 being the default value. A container will shrink its children weighted by the children’s flexShrink
values.
类型 | 必需 |
---|---|
number | 否 |
flexWrap
flexWrap
controls whether children can wrap around after they hit the end of a flex container. It works like flex-wrap
in CSS (default: nowrap). 访问 https://developer.mozilla.org/en-US/docs/Web/CSS/flex-wrap 来进一步了解。Note it does not work anymore with alignItems: stretch
(the default), so you may want to use alignItems: flex-start
for example (breaking change details: https://github.com/facebook/react-native/releases/tag/v0.28.0).
类型 | 必需 |
---|---|
enum('wrap', 'nowrap', 'wrap-reverse') | 否 |
gap
gap
works like gap
in CSS. Only pixel units are supported in React Native. See https://developer.mozilla.org/en-US/docs/Web/CSS/gap for more details.
Type | Required |
---|---|
number | No |
height
height
用于设置组件的高度。
它的表现和 CSS 上的height
类似, but in React Native 只能使用逻辑像素值(数字单位)或百分比,而不能使用 em 或是任何其他单位。 访问 https://developer.mozilla.org/en-US/docs/Web/CSS/height 来进一步了解。
类型 | 必需 |
---|---|
number, string | 否 |
justifyContent
justifyContent
aligns children in the main direction. For example, if children are flowing vertically, justifyContent
controls how they align vertically. It works like justify-content
in CSS (default: flex-start). 访问 https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content 来进一步了解 。
类型 | 必需 |
---|---|
enum('flex-start', 'flex-end', 'center', 'space-between', 'space-around', 'space-evenly') | 否 |
left
left
值是指将本组件定位到距离左边多少个逻辑像素(左边的定义取决于position
属性)。
它的表现和 CSS 上的left
类似, but in React Native 只能使用逻辑像素值(数字单位)或百分比,而不能使用 em 或是任何其他单位。
访问 https://developer.mozilla.org/en-US/docs/Web/CSS/left for more details of how left
affects layout.
类型 | 必需 |
---|---|
number, string | 否 |
margin
设置margin
相同于同时设置marginTop
、marginLeft
、marginBottom
以及marginRight
。访问 https://developer.mozilla.org/en-US/docs/Web/CSS/margin 来进一步了解。
类型 | 必需 |
---|---|
number, string | 否 |
marginBottom
marginBottom
和 CSS 上的margin-bottom
表现一致。访问 https://developer.mozilla.org/en-US/docs/Web/CSS/margin-bottom 来进一步了解。
类型 | 必需 |
---|---|
number, string | 否 |
marginEnd
When direction is ltr
, marginEnd
is equivalent to marginRight
. When direction is rtl
, marginEnd
is equivalent to marginLeft
.
类型 | 必需 |
---|---|
number, string | 否 |
marginHorizontal
设置marginHorizontal
相同于同时设置marginLeft
和marginRight
。
类型 | 必需 |
---|---|
number, string | 否 |
marginLeft
marginLeft
和 CSS 上的margin-left
表现一致。访问 https://developer.mozilla.org/en-US/docs/Web/CSS/margin-left 来进一步了解。
类型 | 必需 |
---|---|
number, string | 否 |
marginRight
marginRight
和 CSS 上的margin-right
表现一致。访问 https://developer.mozilla.org/en-US/docs/Web/CSS/margin-right 来进一步了解。
类型 | 必需 |
---|---|
number, string | 否 |
marginStart
When direction is ltr
, marginStart
is equivalent to marginLeft
. When direction is rtl
, marginStart
is equivalent to marginRight
.
类型 | 必需 |
---|---|
number, string | 否 |
marginTop
marginTop
和 CSS 上的margin-top
表现一致。访问 https://developer.mozilla.org/en-US/docs/Web/CSS/margin-top 来进一步了解。
类型 | 必需 |
---|---|
number, string | 否 |