React native how to wrap content view

2020-05-21 23:52发布

I'm pretty new with RN, is there anyway to wrap the content of the view, similar to Android. In Android I can adjust, Height: 'wrap-content', but somehow in RN, I can't do any wrap content.

It's either I set the height of the view, or just flex, but still not wrap the view.

4条回答
The star\"
2楼-- · 2020-05-22 00:30

You can set the parent to wrap child component like this.

alignSelf: 'baseline'

<View style={{ alignSelf:'baseline'}}>
  <Text>Child Content</Text>
</View>

Wrap child content horizontally.

查看更多
孤傲高冷的网名
3楼-- · 2020-05-22 00:41

In the case of flexDirection: "row", you should use flexWrap: "wrap" to wrap the items inside.

查看更多
放荡不羁爱自由
4楼-- · 2020-05-22 00:42

Views wrap their content by default if 'flex' attribute is not set.

If you need the view to fill parent width or height set 'alignSelf' attribute to "stretch".

查看更多
ゆ 、 Hurt°
5楼-- · 2020-05-22 00:46

If the content is vertical it should wrap by default; If the content is horizontal, things get tricky... What worked for me was putting the view container inside another view:

<View style={{alignItems: "center"}}>
    <View style={{flexDirection: "row"}}>
        <Image/><Text/>
    </View>
</View>
查看更多
登录 后发表回答