我正在学习react native,在我看到的所有教程中,ListView每行仅使用了1个项目.不过,我还没有使用ListView.我只有6个项目必须显示为平面网格,每行2个项目,并且应该是响应性的.我知道这是一个基本的问题,但我也从我这边try 过,从图中可以看出

enter image description here

这是我的密码

 renderDeviceEventList() {
    return _.map(this.props.deviceEventOptions, deviceEventOption => (
        <View key={deviceEventOption.id}>
            <Icon
                name={deviceEventOption.icon_name}
                color="#ddd"
                size={30}
                onPress={() =>
                    this.props.selectDeviceEvent(deviceEventOption)
                }
            />
            <Text style={{ color: "#ff4c4c" }}>
                {deviceEventOption.icon_name}
            </Text>
        </View>
    ));
}
render() {
    return (
        <View
            style={{
                flex: 1,
                top: 60,
                flexDirection: "row",
                justifyContent: "space-around",
                flexWrap: "wrap",
                marginBottom: 10
            }}
        >
            {this.renderDeviceEventList()}
        </View>
    );
}

推荐答案

要使用ListView生成两行网格,可以使用以下代码作为示例:

renderGridItem( item ){
    return (<TouchableOpacity style={styles.gridItem}>
        <View style={[styles.gridItemImage, justifyContent:'center', alignItems:'center'}]}>
            <Text style={{fontSize:25, color:'white'}}>
                {item.fields.name.charAt(0).toUpperCase()}
            </Text>
        </View>
        <Text style={styles.gridItemText}>{item.fields.name}</Text> 
    </TouchableOpacity>
    );
}

renderCategories(){

    var listItems = this.dsinit.cloneWithRows(this.state.dataSource);

    return (
        <ScrollView style={{backgroundColor: '#E8E8E8', flex: 1}} >
            <ListView 
                contentContainerStyle={styles.grid}
                dataSource={listItems}
                renderRow={(item) => this.renderGridItem(item)}
            />
        </ScrollView>
    );
}

const styles = StyleSheet.create({
    grid: {
        justifyContent: 'center',
        flexDirection: 'row',
        flexWrap: 'wrap',
        flex: 1,
    },
    gridItem: {
        margin:5,
        width: 150,
        height: 150,
        justifyContent: 'center',
        alignItems: 'center',
    },
    gridItemImage: {
        width: 100,
        height: 100,
        borderWidth: 1.5, 
        borderColor: 'white',
        borderRadius: 50,
    },
    gridItemText: {
        marginTop: 5,
        textAlign:'center',
    },
});

更改样式以 Select 要在屏幕上看到的行数.这个代码是响应的.

React-native相关问答推荐

Reaction-原生NavBottom设计背景

React Native:任务 ':app:checkDebugDuplicateClasses' 执行失败

更改所选标签栏元素的底部边框 colored颜色

TabNavigator 中的 React Navigation 传递props

在Windows中连接到远程调试器时react-native 超时

使用 Android NDK 在 C 或 C++ 中创建 react-native Native Module?

React-Native 应用程序的 Jest 测试 Animated.View

Undefined 不是判断 this.state.* 的对象

你能在 Ubuntu 上构建 React Native 应用程序(Android 应用程序)吗?

textAlignVertical不是有效的样式属性

React native Touch 事件正在通过绝对视图

React Native - React Navigation导航转换

当前文件夹中的 react-native init

按钮的标题属性必须是字符串

在不使用 3rd 方库的情况下,在react-native中显示主屏幕之前显示启动画面

react-native:webview 高度

react-native:如何覆盖组件中定义的默认样式

如何将图形 API 与 react-native-fbsdk 一起使用?

react-native 开始给出 Invalid 正则表达式无效错误

类型 androidx.appcompat.resources.R$dimen 被定义了多次