我已经阅读了好几篇flexbox教程,但我仍然无法完成这个简单的任务.

我怎样才能使红色的盒子达到100%的宽度?

enter image description here

代码:

  <View style={styles.container}>
    <Text style={styles.welcome}>
      Welcome to React Natives
    </Text>
    <Text style={styles.line1}>
      line1
    </Text>
    <Text style={styles.instructions}>
      Press Cmd+R to reload,{'\n'}
      Cmd+D or shake for dev menu
    </Text>
  </View>

风格:

container: {
  flex: 1,
  justifyContent: 'center',
  alignItems: 'center',
  backgroundColor: '#F5FCFF',
  borderWidth: 1,
  flexDirection: 'column',
},
welcome: {
  fontSize: 20,
  textAlign: 'center',
  margin: 10,
  borderWidth: 1,
},
line1: {
    backgroundColor: '#FDD7E4',
},
instructions: {
  textAlign: 'center',
  color: '#333333',
  marginBottom: 5,
  borderWidth: 1,
},

非常感谢.

Update 1:

输出:

enter image description here

代码:

  <View style={styles.container}>
    <View style={{flex:1}}>
      <Text style={styles.welcome}>
        Welcome to React Natives
      </Text>
    </View>
    <View style={{flex:1}}>
      <Text style={styles.line1}>
        line1
      </Text>
    </View>
    <View style={{flex:1}}>
      <Text style={styles.instructions}>
        Press Cmd+R to reload,{'\n'}
        Cmd+D or shake for dev menu
      </Text>
    </View>
  </View>

  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
    borderWidth: 1,
    flexDirection: 'row',
    flexWrap: 'wrap',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
    borderWidth: 1,
  },
  line1: {
      backgroundColor: '#FDD7E4',
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
    borderWidth: 1,
  },

推荐答案

只需将alignSelf: "stretch"添加到项目的样式表中即可.

line1: {
    backgroundColor: '#FDD7E4',
    alignSelf: 'stretch',
    textAlign: 'center',
},

React-native相关问答推荐

"npx expo run:android命令不能在Expo中运行

错误:图片:找不到 ID 为1的assets资源 .请判断图像源或打包器

在 create-react-native-app 元素中突然看到错误Plugin/Preset files are not allowed to export objects, only functions

React 从 babel 6 到 babel 7 的原生升级

变量 window在react-native 中代表什么?

React Native - 当位置在Android上是绝对时视图消失

在 iOS 模拟器中运行 React Native 应用程序时找不到 UMModuleRegistryAdapter.h

React Native Maps:标记图像不显示在 react-native-maps 中使用自定义标记

元素中缺少main.jsbundle、Foundation和Security,导致报错

将 svg 转换为 react-native-svg

在 React Native (iOS) 中支持动态类型

React Native Expo StackNavigator 重叠通知栏

React Native - React Navigation导航转换

React Native + React (for web) 种子元素

Port 8081 already in use, packager is either not running or not running correctly Command /bin/sh failed with exit code 2

设置 react-native 元素时使用特定的包名称

如何更改键盘上的 return按钮?

尽管 TypeScript 编译器错误,为什么我的 React Native 应用程序构建成功?

在 ReactNative 中单击按钮时如何获取 TextInput 中的值

Gradlew bundleRelease 不会在 react-native 中生成发布 apk