1.指定宽高
最简单的给组件设定尺寸的方式就是在样式中指定固定的width
和height
。React Native 中的尺寸都是无单位的,表示的是与设备像素密度无关的逻辑像素点。
import React, { Component } from 'react';
import { AppRegistry, View } from 'react-native';
export default class FixedDimensionsBasics extends Component {
render() {
return (
<View>
<View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} />
<View style={{width: 100, height: 100, backgroundColor: 'skyblue'}} />
<View style={{width: 150, height: 150, backgroundColor: 'steelblue'}} />
</View>
);
}
}
这样给组件设置尺寸也是一种常见的模式,比如要求在不同尺寸的屏幕上都显示成一样的大小。
2.使用flex布局
在组件样式中使用flex
可以使其在可利用的空间中动态地扩张或收缩。一般而言我们会使用flex:1
来指定某个组件扩张以撑满所有剩余的空间。如果有多个并列的子组件使用了flex:1
,则这些子组件会平分父容器中剩余的空间。如果这些并列的子组件的flex
值不一样,则谁的值更大,谁占据剩余空间的比例就更大(即占据剩余空间的比等于并列组件间flex
值的比)。
3.50%的用法:(指的是子容器占父容器的百分之多少)
mangerBTN: {
flexDirection:'column',
alignItems:'center',
justifyContent:'center',
backgroundColor: '#fff',
width:'50%',
borderRightWidth:0.5,
borderRightColor:'#DCDCDC',
borderBottomWidth:0.5,
borderBottomColor:'#DCDCDC',
},