admin管理员组

文章数量:1435859

I am trying to create a view with a border around it that is centered and fills 80% of the screen. I have e up with this:

<View style={{flexDirection: 'row'}}>
  <View style={{flex: .1}} />
  <View style={{flex: .8, borderWidth: 2}} />
  <View style={{flex: .1}} />
</View>

which works, but seems awfully verbose.

Is there a better (more succinct) way to create a view that is a certain percentage of the screen width and centered in the page?

I am trying to create a view with a border around it that is centered and fills 80% of the screen. I have e up with this:

<View style={{flexDirection: 'row'}}>
  <View style={{flex: .1}} />
  <View style={{flex: .8, borderWidth: 2}} />
  <View style={{flex: .1}} />
</View>

which works, but seems awfully verbose.

Is there a better (more succinct) way to create a view that is a certain percentage of the screen width and centered in the page?

Share Improve this question asked Jul 23, 2016 at 21:38 Abe MiesslerAbe Miessler 85.2k104 gold badges321 silver badges495 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

UPDATE:

Starting from React Native version 0.42 we now have a full percentage support for width, height , padding and so on, see the full list and examples here: https://github./facebook/react-native/mit/3f49e743bea730907066677c7cbfbb1260677d11

Old method:

Consider using Dimensions, like:

import Dimensions from 'Dimensions';

and then in element's style:

width: Dimensions.get('window').width / 100 * 80,

Working example: https://rnplay/apps/4pdwlg

本文标签: javascriptHow to define width by percentage in react nativeStack Overflow