简体   繁体   English

显示空白屏幕-React Native

[英]Blank screen is displayed - React Native

I wrote a simple React Native app which shall display four colored rectangles. 我写了一个简单的React Native应用,它将显示四个彩色矩形。 The app runs well but app is showing only a blank white screen. 该应用程序运行良好,但仅显示空白屏幕。 I replaced the content of the render function with just a text, that is displayed properly. 我只用正确显示的文本替换了render函数的内容。 What is going wrong? 怎么了?

Code provided below: 下面提供的代码:

index.android.js: index.android.js:

import React, { Component } from 'react';
import { Text,
  AppRegistry,
  StyleSheet,
  View
} from 'react-native';

import Mainscreen from './components/screens/mainscreen.js';


 class styling extends Component {
  render() {
    return (
<View>
<Mainscreen />
</View>
);

  }
}
AppRegistry.registerComponent('styling', () => styling);

Mainscreen.js: Mainscreen.js:

import React,{Component} from 'react';
import {View, StyleSheet}  from 'react-native';

export default class Mainscreen extends Component{
  render(){
return (
<View style={styles.container}>
<View style={styles.smallContainer}>
  <View style={styles.above}>
    <View style={styles.leftAbove}></View>
    <View style={styles.rightAbove}></View>
  </View>
  <View style={styles.bottom}>
    <View style={styles.leftBottom}></View>
    <View style={styles.rightBottom}></View>
    </View>
  </View>
</View>
);
}
}
const styles = StyleSheet.create({
  container:{
    flex:1,
    backgroundColor: 'black',
    flexDirection:'column'
  },
  above:{
    flex:1,
    flexDirection:'row',
    marginTop: 10,
    marginLeft: 10,
    marginBottom: 10,
    marginRight: 10
  },
  bottom:
  {
      flex:1,
      flexDirection:'row',
      marginTop: 10,
      marginLeft: 10,
      marginBottom: 10,
      marginRight: 10
  },
leftAbove:{
  backgroundColor: 'green',
  flex: 0.6,
},
rightAbove:{
backgroundColor: 'red',
flex: 0.4,
},
leftBottom:{
backgroundColor: 'blue',
flex: 0.4,
},
rightBottom:{
backgroundColor: 'yellow',
flex: 0.6,
},
smallContainer:{
  flex:1,
  padding: 10,
  backgroundColor:'black'
}
});

Give flex: 1 to your View in styling component (everything else is fine) as follows: flex:1赋予样式中的View组件(其他都可以),如下所示:

class styling extends Component {
    render() {
        return (
            <View style={{ flex: 1 }}>
                <Mainscreen />
            </View>
        );

    }
}

notice the style of flex: 1 in top level view component. 请注意flex的样式顶级视图组件中的1。 It is necessary to tell the view to take the full-screen width and height . 有必要告诉视图采用全屏宽度和高度 Otherwise, height and width will be 0 . 否则, 高度和宽度将为0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM