【问题标题】:Having difficulty in using flexbox in react native在 react native 中使用 flexbox 有困难
【发布时间】:2018-07-13 15:34:42
【问题描述】:

我有这个设计,我试图在本机反应中进行,但无法使用 flexbox 正确安排。

想要的结果

我的代码

import React from 'react';
import { View, Text, StyleSheet } from 'react-native'
import Touchable from 'react-native-platform-touchable';

class Dashboard extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return(
      <View style={styles.container}>
        <View style={styles.row}>
          <Touchable style={styles.widgets}>
            <View><Text> Hello Dashboard </Text></View>
          </Touchable>

          <Touchable style={styles.widgets}>
            <View><Text> Hello Dashboard </Text></View>
          </Touchable>
        </View>

        <View style={styles.row}>
          <Touchable style={styles.widgets}>
            <View><Text> Hello Dashboard </Text></View>
          </Touchable>

          <Touchable style={styles.widgets}>
            <View><Text> Hello Dashboard </Text></View>
          </Touchable>
        </View>

        <View style={styles.row}>
          <Touchable style={styles.widgets}>
            <View><Text> Hello Dashboard </Text></View>
          </Touchable>

          <Touchable style={styles.widgets}>
            <View><Text> Hello Dashboard </Text></View>
          </Touchable>
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'column',
    justifyContent: 'flex-start'
  },
  row: {
    flex: 1,
    flexDirection: 'row',
    height:120,
    marginTop: 15,
    marginBottom: 15,
  },
  widgets: {
    backgroundColor: '#fff',
    elevation: 2,
    width: '44%',
    marginLeft: 15,
    marginRight: 15
  }
})

export default Dashboard;

输出

我认为我的方法有问题。如果有人指出,那就太好了。谢谢。

【问题讨论】:

    标签: css react-native flexbox react-native-flexbox


    【解决方案1】:

    删除小部件样式中的width: '44%' 并将其替换为flex: 1。此外,而不是小部件之间的边距。

    row: {
        flex: 1,
        flexDirection: 'row',
        height:120,
        marginTop: 15,
        marginBottom: 15,
      },
      widgets: {
        backgroundColor: '#fff',
        elevation: 2,
        flex: 1,
        marginLeft: 15,
        marginRight: 15
      }
    

    您的 Expo 应用程序中有一个 Expo 小吃:https://snack.expo.io/@vijayst/widgets

    【讨论】:

    • 在小部件中添加flex:1 会使它们消失。如果我删除flex: 1,那么这些小部件会与它们周围的空格粘在一起
    • 用世博小吃更新了答案。
    猜你喜欢
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多