有可能是一个更好的方式与Flexbox的实现这一点,但我通常定义视宽,视高“百分比”助手vw
和vh
,测量的CSS视口大小单位的名字命名:
import {Dimensions} from 'react-native';
function vw(percentageWidth) {
return Dimensions.get('window').width * (percentageWidth/100);
}
function vh(percentageHeight) {
return Dimensions.get('window').height * (percentageHeight/100);
}
在网格中流动的物品,就可以计算出项目的适当大小,占利润和视口大小:
const COLUMNS = 3;
const MARGIN = vw(1);
const SPACING = (COLUMNS + 1)/COLUMNS * MARGIN;
const grid = {
flex: 1,
flexWrap: 'wrap',
flexDirection: 'row',
justifyContent: 'flex-start'
};
const cell = {
marginLeft: MARGIN,
marginTop: MARGIN,
width: vw(100)/COLUMNS - SPACING
}
return (
<View style={grid}>
{this.props.things.map(thing => <View style={cell} />)}
</View>
)
时,才应使用这个技术,如果你有一个已知的和LIMITE d数量的项目 - 对于任意数量的卡片,出于性能原因,您应该使用ListView
,并将数据集手动分割为行。
你尝试 “flexWrap”? –
是的,但flexWrap只在儿童发生溢出时才起作用,只有在儿童指定宽度属性时才能触发。 @niceass –
指定50%宽度有什么问题?这不正是你想要的吗? –