Make view 80% width of parent in React Native

后端 未结 10 2072
孤城傲影
孤城傲影 2020-12-12 16:49

I\'m creating a form in React Native and would like to make my TextInputs 80% of the screen width.

With HTML and ordinary CSS, this would be straightfor

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

    If you are simply looking to make the input relative to the screen width, an easy way would be to use Dimensions:

    // De structure Dimensions from React
    var React = require('react-native');
    var {
      ...
      Dimensions
    } = React; 
    
    // Store width in variable
    var width = Dimensions.get('window').width; 
    
    // Use width variable in style declaration
    <TextInput style={{ width: width * .8 }} />
    

    I've set up a working project here. Code is also below.

    https://rnplay.org/apps/rqQPCQ

    'use strict';
    
    var React = require('react-native');
    var {
      AppRegistry,
      StyleSheet,
      Text,
      View,
      TextInput,
      Dimensions
    } = React;
    
    var width = Dimensions.get('window').width;
    
    var SampleApp = React.createClass({
      render: function() {
        return (
          <View style={styles.container}>
            <Text style={{fontSize:22}}>Percentage Width In React Native</Text>
            <View style={{marginTop:100, flexDirection: 'row',justifyContent: 'center'}}>
                <TextInput style={{backgroundColor: '#ffffdffffd', height: 60, width: width*.8 }} />
              </View>
          </View>
        );
      }
    });
    
    var styles = StyleSheet.create({
      container: {
        flex: 1,
        marginTop:100
      },
    
    });
    
    AppRegistry.registerComponent('SampleApp', () => SampleApp);
    
    0 讨论(0)
  • 2020-12-12 17:19

    I have an updated solution (late 2019) , to get 80% width of parent Responsively with Hooks it work's even if the device rotate.

    You can use Dimensions.get('window').width to get Device Width in this example you can see how you can do it Responsively

    import React, { useEffect, useState } from 'react';
    import { Dimensions , View , Text , StyleSheet  } from 'react-native';
    
    export default const AwesomeProject() => {
       const [screenData, setScreenData] = useState(Dimensions.get('window').width);
    
        useEffect(() => {
         const onChange = () => {
         setScreenData(Dimensions.get('window').width);
         };
         Dimensions.addEventListener('change', onChange);
    
         return () => {Dimensions.removeEventListener('change', onChange);};
        });
    
       return (  
              <View style={[styles.container, { width: screenData * 0.8 }]}>
                 <Text> I'mAwesome </Text>
               </View>
        );
    }
    
    const styles = StyleSheet.create({
    container: {
         flex: 1,
         alignItems: 'center',
         justifyContent: 'center',
         backgroundColor: '#eee',
         },
    });
    
    0 讨论(0)
  • 2020-12-12 17:20

    Just add quotes around the size in your code. Using this you can use percentage in width, height

    input: {
        width: '80%'
    }
    
    0 讨论(0)
  • 2020-12-12 17:23

    As of React Native 0.42 height: and width: accept percentages.

    Use width: 80% in your stylesheets and it just works.

    • Screenshot

    • Live Example
      Child Width/Height as Proportion of Parent

    • Code

      import React from 'react';
      import { Text, View, StyleSheet } from 'react-native';
      
      const width_proportion = '80%';
      const height_proportion = '40%';
      
      const styles = StyleSheet.create({
        screen: {
          flex: 1,
          alignItems: 'center',
          justifyContent: 'center',
          backgroundColor: '#5A9BD4',
        },
        box: {
          width: width_proportion,
          height: height_proportion,
          alignItems: 'center',
          justifyContent: 'center',
          backgroundColor: '#B8D2EC',
        },
        text: {
          fontSize: 18,
        },
      });
      
      export default () => (
        <View style={styles.screen}>
          <View style={styles.box}>
            <Text style={styles.text}>
              {width_proportion} of width{'\n'}
              {height_proportion} of height
            </Text>
          </View>
        </View>
      );
      
    0 讨论(0)
  • 2020-12-12 17:23

    That should fit your needs:

    var yourComponent = React.createClass({
        render: function () {
            return (
                <View style={{flex:1, flexDirection:'column', justifyContent:'center'}}>
                    <View style={{flexDirection:'row'}}>
                        <TextInput style={{flex:0.8, borderWidth:1, height:20}}></TextInput>
                        <View style={{flex:0.2}}></View> // spacer
                    </View>
                </View>
            );
        }
    });
    
    0 讨论(0)
  • 2020-12-12 17:23

    This is the way I got the solution. Simple and Sweet. Independent of Screen density:

    export default class AwesomeProject extends Component {
        constructor(props){
            super(props);
            this.state = {text: ""}
        }
      render() {
        return (
           <View
              style={{
                flex: 1,
                backgroundColor: "#ececec",
                flexDirection: "column",
                justifyContent: "center",
                alignItems: "center"
              }}
            >
              <View style={{ padding: 10, flexDirection: "row" }}>
                <TextInput
                  style={{ flex: 0.8, height: 40, borderWidth: 1 }}
                  onChangeText={text => this.setState({ text })}
                  placeholder="Text 1"
                  value={this.state.text}
                />
              </View>
              <View style={{ padding: 10, flexDirection: "row" }}>
                <TextInput
                  style={{ flex: 0.8, height: 40, borderWidth: 1 }}
                  onChangeText={text => this.setState({ text })}
                  placeholder="Text 2"
                  value={this.state.text}
                />
              </View>
              <View style={{ padding: 10, flexDirection: "row" }}>
                <Button
                  onPress={onButtonPress}
                  title="Press Me"
                  accessibilityLabel="See an Information"
                />
              </View>
            </View>
        );
      }
    }
    
    0 讨论(0)
提交回复
热议问题