如何在 React Native 中使用九方形视图进行布局?
How to layout with nine square view in React Native?
如何实现如下图的正方形布局?
或者是否存在任何相关包?
我正在使用 ScrollView 和 flexbox 的组合来实现我的静态网格视图。
import {Dimensions} from 'react-native';
....
return (
<ScrollView>
<View style={styles.container}>
{
this.props.categories.map((category, index) => {
return (
<TouchableOpacity
key={index}
style={styles.item}
onPress={() => {}}
>
<Image
style={styles.itemIcon}
source="..."
/>
<Text style={styles.itemTitle}>
{category.name}
</Text>
</TouchableOpacity>
)
})
}
</View>
</ScrollView>
)
var styles = StyleSheet.create({
container: {
flexDirection: 'row',
flexWrap: 'wrap'
},
item: {
width: Dimensions.get('window').width * 0.5,
height: 100,
borderWidth: 1,
borderColor: "lightgray",
alignItems: 'center',
justifyContent: 'center'
},
itemIcon: {
width: 100,
height: 100,
resizeMode: 'contain'
},
itemTitle: {
marginTop: 16,
},
});
我在谷歌上搜索了同样的问题并找到了一个更简单的解决方案。只需使用 aspectRatio: 1
样式属性。看一个例子:https://reactnative.fun/2017/06/21/ratio/
如何实现如下图的正方形布局?
或者是否存在任何相关包?
我正在使用 ScrollView 和 flexbox 的组合来实现我的静态网格视图。
import {Dimensions} from 'react-native';
....
return (
<ScrollView>
<View style={styles.container}>
{
this.props.categories.map((category, index) => {
return (
<TouchableOpacity
key={index}
style={styles.item}
onPress={() => {}}
>
<Image
style={styles.itemIcon}
source="..."
/>
<Text style={styles.itemTitle}>
{category.name}
</Text>
</TouchableOpacity>
)
})
}
</View>
</ScrollView>
)
var styles = StyleSheet.create({
container: {
flexDirection: 'row',
flexWrap: 'wrap'
},
item: {
width: Dimensions.get('window').width * 0.5,
height: 100,
borderWidth: 1,
borderColor: "lightgray",
alignItems: 'center',
justifyContent: 'center'
},
itemIcon: {
width: 100,
height: 100,
resizeMode: 'contain'
},
itemTitle: {
marginTop: 16,
},
});
我在谷歌上搜索了同样的问题并找到了一个更简单的解决方案。只需使用 aspectRatio: 1
样式属性。看一个例子:https://reactnative.fun/2017/06/21/ratio/