Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

100% width in React Native Flexbox

People also ask

How do I get the screen width in React Native?

You can get the application window's width and height using the following code: const windowWidth = Dimensions. get('window').

How do you set dynamic height and width in React Native?

In react-native, to set the dynamic width and height to a component, we use built-in interface Dimensions. it takes the dimension of a given component and then sets the width and height to its equivalent.


Simply add alignSelf: "stretch" to your item's stylesheet.

line1: {
    backgroundColor: '#FDD7E4',
    alignSelf: 'stretch',
    textAlign: 'center',
},

You should use Dimensions

First, define Dimensions.

import { Dimensions } from "react-native";

var width = Dimensions.get('window').width; //full width
var height = Dimensions.get('window').height; //full height

then, change line1 style like below:

line1: {
    backgroundColor: '#FDD7E4',
    width: width,
},

Editted:

In order to flex only the center text, a different approach can be taken - Unflex the other views.

  • Let flexDirection remain at 'column'
  • remove the alignItems : 'center' from container
  • add alignSelf:'center' to the textviews that you don't want to flex

You can wrap the Text component in a View component and give the View a flex of 1.

The flex will give :

100% width if the flexDirection:'row' in styles.container

100% height if the flexDirection:'column' in styles.container


Here you go:

Just change the line1 style as per below:

line1: {
    backgroundColor: '#FDD7E4',
    width:'100%',
    alignSelf:'center'
}