React Native学习(5)组件的高度与宽度

React Native学习(5)组件的高度与宽度

指定宽高

就是直接赋予组件widthheight,React Native 中的尺寸都是无单位的,表示的是与设备像素密度无关的逻辑像素点。

/**
 * 慢慢学习
 * https://blog.csdn.net/quanhaoH
 */

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

export default class MyApp extends Component {
  render() {
	  return (
      <View>
        <View style={styles.red} />
        <View style={styles.bigBlue} />
        <View style={styles.black} />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  red: {
	width: 50,
	height: 50,
    backgroundColor: 'red',
  },
  bigBlue: {
	width: 100,
	height: 100,
    backgroundColor: 'blue',
  },
  black: {
    width: 150,
	height: 150,
	backgroundColor: 'black',
  },
});

AppRegistry.registerComponent('MyApp', () => MyApp);

在这里插入图片描述
这样给组件设置尺寸也是一种常见的模式,比如要求在不同尺寸的屏幕上都显示成一样的大小。

弹性(Flex)宽高

在组件样式中使用flex可以使其在可利用的空间中动态地扩张或收缩。一般而言我们会使用flex:1来指定某个组件扩张以撑满所有剩余的空间。则这些子组件会平分父容器中剩余的空间。如果这些并列的子组件的flex值不一样,则谁的值更大,谁占据剩余空间的比例就更大。
如:

/**
 * 慢慢学习
 * https://blog.csdn.net/quanhaoH
 */

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

export default class MyApp extends Component {
  render() {
	  return (
      <View style={{flex: 1}}>
        <View style={styles.red} />
        <View style={styles.bigBlue} />
        <View style={styles.black} />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  red: {
	flex: 1,
	width: 50,
	height: 50,
    backgroundColor: 'red',
  },
  bigBlue: {
	flex: 2,
	width: 100,
	height: 100,
    backgroundColor: 'blue',
  },
  black: {
	flex: 13,
    width: 150,
	height: 150,
	backgroundColor: 'black',
  },
});

AppRegistry.registerComponent('MyApp', () => MyApp);

在这里插入图片描述
组件能够撑满剩余空间的前提是其父容器的尺寸不为零。如果父容器既没有固定的width和height,也没有设定flex,则父容器的尺寸为零。其子组件如果使用了flex,也是无法显示的。
比如这样父视图尺寸为0。子组件还使用了flex,则子组件就无法显示了

<View>
        <View style={styles.red} />
        <View style={styles.bigBlue} />
        <View style={styles.black} />
</View>

父视图使用指定宽高也是可以的
如:

/**
 * 慢慢学习
 * https://blog.csdn.net/quanhaoH
 */

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

export default class MyApp extends Component {
  render() {
	  return (
      <View style={{width:300, height:400, backgroundColor: 'yellow'}}>
        <View style={styles.red} />
        <View style={styles.bigBlue} />
        <View style={styles.black} />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  red: {
	flex: 1,
	width: 50,
	height: 50,
    backgroundColor: 'red',
  },
  bigBlue: {
	flex: 2,
	width: 100,
	height: 100,
    backgroundColor: 'blue',
  },
  black: {
	flex: 13,
    width: 150,
	height: 150,
	backgroundColor: 'black',
  },
});

AppRegistry.registerComponent('MyApp', () => MyApp);

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄权浩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值