【问题标题】:flex:1 not filling whole screenflex:1 没有填满整个屏幕
【发布时间】:2018-12-05 11:40:44
【问题描述】:

我正在尝试更改我的应用的背景颜色,但颜色并没有填满整个屏幕。当我将 flex 设置为 0 时,背景颜色仅跨越所需的数量,但当我将其设置为 1 时,它不会扩大整个屏幕,而是缩小。我做错了什么?

这里是代码,改变根视图 flex: 使成为() { 返回 (

    <Text>{welcome}</Text>

    <TextInput
      underlineColorAndroid="rgba(0,0,0,0)"
      placeholder={email}
      autoCapitalize="none"
      style={styles.textInput}
      onChangeText={email1 => this.setState({ email1 })}
    />
    <TextInput
      underlineColorAndroid="rgba(0,0,0,0)"
      secureTextEntry
      placeholder={password}
      autoCapitalize="none"
      style={styles.textInput}
      onChangeText={password1 => this.setState({ password1 })}
      value={this.state.password1}
    />
    {this.state.errorMessage && (
      <Text style={styles.error}>{this.state.errorMessage}</Text>
    )}
    <TouchableOpacity
      onPress={this.handleSignUp}
      style={styles.buttonContainer}
    >
      <Text style={styles.buttonText}>{signup}</Text>
    </TouchableOpacity>

    <Text
      style={styles.text}
      onPress={() => this.props.navigation.navigate("LoginScreen")}
    >
      {already_have_an_account}
    </Text>
  </KeyboardAvoidingView>
);

} }

    const styles = StyleSheet.create({
    container: {
      justifyContent: "center",
      padding: 20,
      backgroundColor: "red",
      flex: 0
    },
    textInput: {
      height: 40,
      marginTop: 8,
     paddingHorizontal: 8,
      backgroundColor: "#e2e2e2"
    },
    buttonContainer: {
      backgroundColor: "#3bd774",
      padding: 15,
      marginTop: 10
    },

  buttonText: {
    textAlign: "center",
    color: "white",
    fontWeight: "bold"
  },

  logo: {
    width: 200,
    height: 200
  },

  logoContainer: {
    alignItems: "center",
    justifyContent: "center"
  },

  text: {
    marginTop: 16,
    color: "#bcbcbc",
    textAlign: "center"
  },

  error: {
    marginTop: 8,
    color: "red"
  }
});

【问题讨论】:

标签: javascript css react-native flexbox


【解决方案1】:

您需要确保在视图链中一直有 flex:1。这是因为 flex: 1 将使视图填充其父视图

我可以看到您复制/粘贴的不是所有代码,因为有一个 &lt;/KeyboardAvoidingView&gt; 但没有开始标签。

所以,至少你需要一个&lt;KeyboardAvoidingView style={{flex: 1}}&gt;,但如果有东西包裹它,你也应该将 flex:1 添加到该控件。

【讨论】:

    【解决方案2】:

    将整个布局放入 View 包装器中,并为该视图赋予此样式

    container: {
          justifyContent: "center",
          padding: 20,
          alignItems:'center',
          backgroundColor: "red",
          flex: 1
        }
    

    运行示例:https://snack.expo.io/Hy-Gv-lGm

    【讨论】:

      猜你喜欢
      • 2020-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-19
      • 1970-01-01
      相关资源
      最近更新 更多