Make view 80% width of parent in React Native

后端 未结 10 2116
孤城傲影
孤城傲影 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: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 (  
              
                  I'mAwesome 
               
        );
    }
    
    const styles = StyleSheet.create({
    container: {
         flex: 1,
         alignItems: 'center',
         justifyContent: 'center',
         backgroundColor: '#eee',
         },
    });
    

提交回复
热议问题