有可能是一個更好的方式與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%寬度有什麼問題?這不正是你想要的嗎? –