ios - 在 native react 中如何安排图像按钮

标签 ios reactjs react-native

我是 React Native 的新手。

我需要在 4 行和 2 列中排列 8 个图像按钮。我尝试使用 flexDirection,但它不起作用(混淆)。
实际上,我需要像下图那样在 native react 中安排我的 8 张图片。(我只需要 4 行,2 列)

请帮帮我..

enter image description here 我的代码

` 容器:{ 背景颜色:'#ccffcc',

        flex:1,
        flexDirection: ('row'),
        //paddingTop: 40,
        //alignItems :'center',
        padding:10

      },
      button1:{
         flex:1,
        // width : 100,
        // height: 100,
        flexDirection:'row',
        backgroundColor : '#ff9933',
        //alignSelf:'flex-start',
        marginTop: 70,
        margin: 20,
        //justifyContent:'center'
      },
      button2:{

        //flex:1,
        //flexDirection:'column',
        alignSelf:'center',
        //height :50,
        //backgroundColor : '#ff9933',
        //alignSelf:'flex-start',
        margin: 20,
        marginTop:-30,
        //justifyContent:'center'
      },
      button3:{
        //flex:1,
        flexDirection: 'column',
        // width : 100,
        // height: 100,
        backgroundColor : '#ff9933',
        alignSelf:'flex-start',
      //  marginTop:100,
        margin: 20,

        justifyContent:'center'
      },
      button4:{
        //flex:1,
      //  flexDirection: 'row',
        //height :50,
        //backgroundColor : '#ff9933',
        alignSelf:'flex-start',
        margin: 20,
      //  marginTop:30,
        justifyContent:'center'
      },
      button5:{
        flex:1,
        flexDirection:'column',
    // width : 100,
        // height: 100,
        backgroundColor : '#ff9933',
        alignSelf:'flex-end',
        //marginTop:50,
        margin: 20,

        justifyContent:'center'
      },
      button6:{
        flexDirection:'column',
        flex:1,
        //height :50,
        //backgroundColor : '#ff9933',
        alignSelf:'flex-end',
        margin: 20,
      //  marginTop:30,
        justifyContent:'center'
      },
      button7:{
        flexDirection:'column',
        flex:1,
       // width : 100,
        // height: 100,
        backgroundColor : '#ff9933',
        alignSelf:'flex-end',
      //  marginTop:100,
        margin: 20,

        justifyContent:'center'
      },
      button8:{
        flexDirection:'column',
        flex:1,    //height :50,
        //backgroundColor : '#ff9933',
        alignSelf:'flex-end',
        margin: 20,
      //  marginTop:30,
        justifyContent:'center'
      },`

最佳答案

您可以通过计算容器的宽度,然后简单地将宽度除以每行所需的项目数来做到这一点:

enter image description here

查看 this我设置的示例,下面也粘贴了代码。

 "use strict";

var React = require("react-native");
var { AppRegistry, StyleSheet, Text, View, Dimensions, ScrollView } = React;

var width = Dimensions.get("window").width - 20;
var images = [1, 2, 3, 4, 5, 6, 7, 8];

var SampleApp = React.createClass({
render: function() {
    var icons = images.map((image, i) => {
        return (
            <View key={i} style={styles.listItemContainer}>
                <View style={styles.listItem}>
                    <Text style={{ backgroundColor: "transparent" }}>
                        Image {image}
                    </Text>
                </View>
            </View>
        );
    });

    return (
        <ScrollView style={{ flex: 1, paddingLeft: 10, paddingRight: 10 }}>
            <View style={styles.container}>
                <View style={styles.list}>{icons}</View>
            </View>
        </ScrollView>
    );
}
});

var styles = StyleSheet.create({
container: {
    flex: 1
},
list: {
    flexDirection: "row",
    flexWrap: "wrap",
    flex: 1,
    marginTop: 50
},
listItemContainer: {
    width: width / 4,
    height: width / 4
},
listItem: {
    backgroundColor: "#ededed",
    borderRadius: 15,
    margin: 10,
    flex: 1
}
});

AppRegistry.registerComponent("SampleApp", () => SampleApp);

关于ios - 在 native react 中如何安排图像按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35403313/

相关文章:

ios - CoreAudio : Ausampler Unit polyphony

ios - 为团队 Mac 创建证书并仅授权自己创建配置文件

javascript - ReactJs - 当特定的 NavLink 处于事件状态时在元素上插入新的 css 类

react-native - React Native 中可调整大小的 Flex 布局

reactjs - react native 日历

ios - iOS 设备上的 Expo.io 离线测试

ios - 如何获取有关哪个应用程序是iOS前台的信息?

ios - 当您使用 App Store Connect 从 App Store 中删除一个应用程序时,更改需要多长时间?

javascript - 页面未重定向到更新的 URL

reactjs - 错误和加载是否应该存储在 store 或 state 中?