React Native Flexbox中的100%宽度

fra*_*ran 184 flexbox react-native

我已经阅读了几个flexbox教程,但我仍然无法使这个简单的任务工作.

如何将红色框设为100%宽度?

在此输入图像描述

码:

  <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>
Run Code Online (Sandbox Code Playgroud)

样式:

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,
},
Run Code Online (Sandbox Code Playgroud)

谢谢!

更新1: Nishanth Shankar的建议,为包装器添加flex:1,flexDirection:'row'

输出:

在此输入图像描述

码:

  <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,
  },
Run Code Online (Sandbox Code Playgroud)

Cor*_* S. 377

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

line1: {
    backgroundColor: '#FDD7E4',
    alignSelf: 'stretch',
    textAlign: 'center',
},
Run Code Online (Sandbox Code Playgroud)

  • 链接坏了. (9认同)
  • @st_bk在那里是救生员!但是,我为您绝对定位并需要伸展的事件找到了更好的解决方案:`position:absolute,top:0,bottom:0,left:0,right:0` (3认同)
  • 使用 alignSelf 拉伸,我遇到了 Android 的问题 - 我有一个带有“绝对”位置的图像,在这种情况下,即使使用“拉伸”,这样的框也会被填充到其中包含的元素的边缘。Dimensions.get('window').width 在我的例子中适用于 iOS 和 Android。 (2认同)
  • 看起来 `width: "100%"` 也应该有效,但它没有。我想我不明白什么时候 `alignSelf: "stretch"` 与 `width: "100%"` 工作。@Corentin 有什么想法吗?谢谢! (2认同)

Mel*_*cuk 106

您应该使用Dimensions

首先,定义Dimensions.

import { Dimensions } from "react-native";

var width = Dimensions.get('window').width; //full width
var height = Dimensions.get('window').height; //full height
Run Code Online (Sandbox Code Playgroud)

然后,改变line1如下的风格:

line1: {
    backgroundColor: '#FDD7E4',
    width: width,
},
Run Code Online (Sandbox Code Playgroud)

  • 旋转设备时,这将不起作用.最好使用自适应布局. (6认同)
  • @peacepassion,你有360,因为你的设备dpi缩放了2倍.尝试`Dimensions.get('window').scale` - 在你的情况下应该返回2. (4认同)
  • Melih Mucuk!alignSelf:Corentin S.建议的"拉伸"是最容易和最简单的,所以我接受了这个答案.但是,拥有Dimensions会让我找到一种围绕布局的新方法.在某些情况下,我们可以通过编程方式调整项目,谢谢! (3认同)

Nis*_*kar 24

Editted:

为了仅弯曲中心文本,可以采用不同的方法 - 解开其他视图.

  • 让flexDirection保持在'列'
  • alignItems : 'center'从容器中删除
  • 添加alignSelf:'center'到您不想弯曲的textviews

您可以将Text组件包装在View组件中,并为View提供1的flex.

flex将给出:

如果flexDirection:'row'在styles.container中,则为100%宽度

如果flexDirection:'column'在styles.container中,则为100%高度


小智 6

使用javascript获取宽度和高度,并以View的样式添加它们。要获得完整的宽度和高度,请使用Dimensions.get('window').width https://facebook.github.io/react-native/docs/dimensions.html

getSize() {
    return {
        width: Dimensions.get('window').width, 
        height: Dimensions.get('window').height
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,

<View style={[styles.overlay, this.getSize()]}>
Run Code Online (Sandbox Code Playgroud)


iDe*_*mit 6

干得好:

只需按照以下方式更改line1样式:

line1: {
    backgroundColor: '#FDD7E4',
    width:'100%',
    alignSelf:'center'
}
Run Code Online (Sandbox Code Playgroud)

  • 我很高兴这有效!我不知道为什么,我认为百分比值在 RN 中不起作用...... (4认同)

小智 5

首先添加Dimension组件:

import { AppRegistry, Text, View,Dimensions } from 'react-native';
Run Code Online (Sandbox Code Playgroud)

第二个定义变量:

var height = Dimensions.get('window').height;
var width = Dimensions.get('window').width;
Run Code Online (Sandbox Code Playgroud)

第三把它放在样式表中:

textOutputView: {
    flexDirection:'row',
    paddingTop:20,
    borderWidth:1,
    borderColor:'red',
    height:height*0.25,
    backgroundColor:'darkgrey',
    justifyContent:'flex-end'
}
Run Code Online (Sandbox Code Playgroud)

实际上,在此示例中,我想制作响应式视图,并且只想查看屏幕视图的0.25,所以我将其乘以0.25,如果您希望屏幕的100%不要将其与以下任何内容相乘:

textOutputView: {
    flexDirection:'row',
    paddingTop:20,
    borderWidth:1,
    borderColor:'red',
    height:height,
    backgroundColor:'darkgrey',
    justifyContent:'flex-end'
}
Run Code Online (Sandbox Code Playgroud)