css - 在 React Native 中使用 Flexbox 有困难

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

我有这个设计,我试图在 React Native 中制作,但无法使用 flexbox 正确安排。

期望的结果

The design .

我的代码

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;

输出

Resultant design

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

最佳答案

移除widgets样式中的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

关于css - 在 React Native 中使用 Flexbox 有困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48594446/

相关文章:

html - Flex 框在本应是行的情况下呈现为列

html - 你如何使 anchor 标签居中?

react-native - 如何将组件中的 props 传递给 FlatList renderItem

node.js - 如何在react-native应用程序中实现NTLM身份验证?

html - 如何使用flexbox垂直居中div

html - CSS Flex 基础孙子

html - 是否可以从 CSS 中的 attr() 获取正则表达式?

css 在 ie8/9 中被忽略,不在选择器限制

javascript - 使用 javascript if/else 更改 div 背景图像

javascript - 在 React Native 上调节按钮