100% width in React Native Flexbox

后端 未结 10 889
日久生厌
日久生厌 2020-12-12 11:51

I have already read several flexbox tutorial, but I still cannot make this simple task to work.

How can I make the red box to 100% width?

Code:

相关标签:
10条回答
  • 2020-12-12 12:30

    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,
    },
    
    0 讨论(0)
  • 2020-12-12 12:30

    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

    0 讨论(0)
  • 2020-12-12 12:41

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

    line1: {
        backgroundColor: '#FDD7E4',
        alignSelf: 'stretch',
        textAlign: 'center',
    },
    
    0 讨论(0)
  • 2020-12-12 12:44

    First add Dimension component:

    import { AppRegistry, Text, View,Dimensions } from 'react-native';
    

    Second define Variables:

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

    Third put it in your stylesheet:

    textOutputView: {
        flexDirection:'row',
        paddingTop:20,
        borderWidth:1,
        borderColor:'red',
        height:height*0.25,
        backgroundColor:'darkgrey',
        justifyContent:'flex-end'
    }
    

    Actually in this example I wanted to make responsive view and wanted to view only 0.25 of the screen view so I multiplied it with 0.25, if you wanted 100% of the screen don't multiply it with any thing like this:

    textOutputView: {
        flexDirection:'row',
        paddingTop:20,
        borderWidth:1,
        borderColor:'red',
        height:height,
        backgroundColor:'darkgrey',
        justifyContent:'flex-end'
    }
    
    0 讨论(0)
  • 2020-12-12 12:44

    Noted: Try to fully understanding about flex concept.

           <View style={{
              flex: 2,
              justifyContent: 'center',
              alignItems: 'center'
            }}>
              <View style ={{
                  flex: 1,
                  alignItems: 'center, 
                  height: 50, 
                  borderWidth: 1, 
                  borderColor: '#000' 
              }}>
                   <Text>Welcome to React Nativ</Text>
               </View>
               <View style={{
                  flex: 1,
                  alignItems: 'center,
                  borderWidth: 1, 
                  borderColor: 'red ', 
                  height: 50
                }}
                >
                  <Text> line 1 </Text>
                </View>
              <View style={{
                flex: 1,
                alignItems: 'center, 
                height: 50, 
                borderWidth: 1,                     
                borderColor: '#000'
              }}>
                 <Text>
                  Press Cmd+R to reload,{'\n'}
                  Cmd+D or shake for dev menu
                 </Text>
               </View>
           </View>
    
    0 讨论(0)
  • 2020-12-12 12:47
    Style ={{width : "100%"}}
    

    try this:

    StyleSheet generated: {
      "width": "80%",
      "textAlign": "center",
      "marginTop": 21.8625,
      "fontWeight": "bold",
      "fontSize": 16,
      "color": "rgb(24, 24, 24)",
      "fontFamily": "Trajan Pro",
      "textShadowColor": "rgba(255, 255, 255, 0.2)",
      "textShadowOffset": {
        "width": 0,
        "height": 0.5
      }
    }
    
    0 讨论(0)
提交回复
热议问题