【问题标题】:Flex:1 not making parent <View> element cover entire screen, instead shrinks all elements to top of the screenFlex:1 不让父 <View> 元素覆盖整个屏幕,而是将所有元素缩小到屏幕顶部
【发布时间】:2019-10-28 16:17:16
【问题描述】:

我正在学习 react-native,并且希望 Parent 使用 flex 而不是静态的高度/宽度值动态地占据整个屏幕。它适用于 {height:714, width:393},但是当我用 flex:1 替换它们时,所有元素都会缩小到屏幕顶部。

Code

样式:

const 样式 = StyleSheet.create({ 容器: { 背景颜色:'紫色', 填充:5, // 弹性:1, 身高:714, 宽度:393, },

bigBlackText: {
    color: 'black',
    fontSize: 18,
},

bigWhiteText: {
    color: 'white',
    fontSize: 19,
},

col1: {
    backgroundColor: 'yellow',
    flexDirection: 'row',
    flex: 1,
},

col11: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    margin: 5,
    backgroundColor: 'orange',
},

col12: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    margin: 5,
    backgroundColor: 'orange',
},

col13: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    margin: 5,
    backgroundColor: 'orange',
},

col2: {
    flex: 1,
    backgroundColor: 'red',
},

col21: {
    flex: 1,
    justifyContent: 'space-around',
    alignItems: 'center',
    backgroundColor: 'brown',
    margin: 5
},

col22: {
    flex: 1,
    justifyContent: 'space-around',
    alignItems: 'center',
    backgroundColor: 'brown',
    margin: 5
},

col23: {
    flex: 1,
    justifyContent: 'space-around',
    alignItems: 'center',
    backgroundColor: 'brown',
    margin: 5
},

col3: {
    flex: 1,
    backgroundColor: 'cyan',
    flexDirection: 'column',
},

col31: {
    flexDirection: 'row',
    flex: 1,
},  

col31r1: {
    flex: 1,
    justifyContent: 'space-around',
    alignItems: 'center',
    backgroundColor: 'green',
    margin: 5,
},

col31r2: {
    flex: 1,
    justifyContent: 'space-around',
    alignItems: 'center',
    backgroundColor: 'green',
    margin: 5,
},

col32: {
    flexDirection: 'row',
    flex: 1,
},

col32r1: {
    flex: 1,
    justifyContent: 'space-around',
    alignItems: 'center',
    backgroundColor: 'green',
    margin: 5,
},  

col32r2: {
    flex: 1,
    justifyContent: 'space-around',
    alignItems: 'center',
    backgroundColor: 'green',
    margin: 5,
},

});

Expected Output

Actual Output

【问题讨论】:

    标签: react-native react-native-android


    【解决方案1】:

    我发现没有问题。不要在容器样式表中使用justifycontentalignitems 道具。

    【讨论】:

    • 是的,问题似乎只在我的系统上,实际上只在最高(父)视图元素中。仅当我将完整的设备高度和宽度值分配给父级时,它才有效。你觉得跟版本有关系吗?谢谢!
    • 我正在为 Android 平台开发 Ubuntu。但这个问题似乎在较新的项目上得到了解决。无论如何谢谢!
    【解决方案2】:

    尝试设置宽度和高度

    mainView: {
       width: "100%",
       height: "100%"
    }
    

    或者,

    mainView: {
       width: deviceWidht,
       height: deviceHeight
    }
    

    我希望这行得通。

    【讨论】:

    • 非常感谢,效果很好,但我还是不明白为什么 flex 属性会搞乱所有样式。
    • flex: 1 覆盖其容器的整个宽度和高度。可能这就是原因。
    • 您能详细说明一下吗?在我的示例中,我希望覆盖容器的整个宽度和高度,但它只是让所有内容都变为空白。将宽度设置为“100%”虽然有效。
    【解决方案3】:
    import React from 'react';
    import { View, StyleSheet, Text } from 'react-native';
    export default class ViewDemo extends React.Component {
    
        render() {
            return (
                <View style={styles.container}>
    
                    <View style={{ flex: 1, backgroundColor: 'yellow', flexDirection: 'row', justifyContent: 'space-around' ,flexWrap: 'wrap'}}>
                        <View style={styles.h1}></View>
                        <View style={styles.h1}></View>
                        <View style={styles.h1}></View>
                    </View>
    
                    <View style={{ flex: 1, backgroundColor: 'red', flexDirection: 'column', justifyContent: 'space-around' }}>
                        <View style={styles.h2}></View>
                        <View style={styles.h2}></View>
                        <View style={styles.h2}></View>
                    </View>
    
                    <View style={{ flex: 1, alignItem:'center' , backgroundColor: '#3CAEA3', flexDirection: 'row', flexWrap: 'wrap', justifyContent: 'space-around' }}>
                        <View style={styles.h3}></View>
                        <View style={styles.h3}></View>
                        <View style={styles.h3}></View>
                        <View style={styles.h3}></View>
                    </View>
                </View>
            );
        }
    
    }
    
    const styles = StyleSheet.create({
        container: {
            flex: 1,
        },
    
        h1: {
            backgroundColor: '#f79c1d',
            width: 100,
            justifyContent : 'space-around',
            margin: 10
        },
    
        h2: {
            backgroundColor: '#9c2c2c',
            height: 50,
            justifyContent: 'space-around',
            margin: 10
        },
    
        h3: {
            backgroundColor: '#616f39',
            height: 90,
            width: 180,
            margin: 10
        },
    
    
    });
    

    你可以直接使用这段代码

    【讨论】:

    • 我复制粘贴了代码,输出又被弄乱了:(。在我用 height:"100%", width:"100%" 替换容器样式上的 flex:1 后它工作了. 你知道为什么 flex:1 不起作用吗?非常感谢。
    猜你喜欢
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多