Make view 80% width of parent in React Native

后端 未结 10 2099
孤城傲影
孤城傲影 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: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 () => (
        
          
            
              {width_proportion} of width{'\n'}
              {height_proportion} of height
            
          
        
      );
      

提交回复
热议问题