最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React Native - centering flexWrap content - Stack Overflow

programmeradmin2浏览0评论

I have a View which needs to render items in a list. The items need to be rendered in a row and then wrap around. I am able to achieve this behavior by using flexDirection and flexWrap as below. The problem is that the wrapped rows all appear left justified thus leaving an undetermined space towards the right. This makes some sense but I'm wondering if there is a way to center the content in each row that flexWrap creates?

<View style={{alignItems: 'center', justifyContent: 'center'}}>
  <View style={{alignItems: 'center', justifyContent: 'center', flexDirection: 'row', flexWrap: 'wrap'}}>
    <Item value={1} />
    <Item value={2} />
    <Item value={3} />
  </View>
</View>

I have a View which needs to render items in a list. The items need to be rendered in a row and then wrap around. I am able to achieve this behavior by using flexDirection and flexWrap as below. The problem is that the wrapped rows all appear left justified thus leaving an undetermined space towards the right. This makes some sense but I'm wondering if there is a way to center the content in each row that flexWrap creates?

<View style={{alignItems: 'center', justifyContent: 'center'}}>
  <View style={{alignItems: 'center', justifyContent: 'center', flexDirection: 'row', flexWrap: 'wrap'}}>
    <Item value={1} />
    <Item value={2} />
    <Item value={3} />
  </View>
</View>
Share Improve this question edited Jan 21, 2017 at 16:10 Anshul Kai asked Dec 1, 2016 at 22:18 Anshul KaiAnshul Kai 4,1383 gold badges32 silver badges55 bronze badges 3
  • you can play around with flex here and find the right bination of values to achieve what you want the-echoplex/flexyboxes – diedu Commented Dec 2, 2016 at 0:18
  • Have you tried adding justifyContent: 'center'? That would be the property to use for horizontal alignment in flexDirection: 'row'. – Michael Benjamin Commented Dec 2, 2016 at 0:46
  • Thanks for sharing the link on flexyboxes. A very handy tool. I was able to get it working on flexyboxes by setting justifyContent and alignContent to center but still have no luck in my project. FWIW, React Native doesn't support alignContent and adding justifyContent to my View seems to have no effect. – Anshul Kai Commented Dec 3, 2016 at 15:15
Add a ment  | 

1 Answer 1

Reset to default 7

The external alignItems somehow gets in the way. I'm not sure about the inner workings and why this works but the code below now works as expected.

<View style={{justifyContent: 'center'}}>
  <View style={{alignItems: 'center', justifyContent: 'center', flexDirection: 'row', flexWrap: 'wrap'}}>
    <Item value={1} />
    <Item value={2} />
    <Item value={3} />
  </View>
</View>
发布评论

评论列表(0)

  1. 暂无评论