【问题标题】:Rendering React Native 'Tile' in a single row在一行中渲染 React Native 'Tile'
【发布时间】:2018-07-29 05:39:54
【问题描述】:

我是开发 React Native 应用程序的新手。我想在一行中对齐多个(可能是三个)React Native Element 'Tile',类似于 2X3 矩阵样式。我正在尝试使用 flexbox,但无法达到结果。 这是我的代码:

import React, { Component } from 'react';
import { Text, View, TextInput, StyleSheet } from 'react-native';
import { Constants } from 'expo';
import { Tile } from 'react-native-elements';

export default class Tel extends Component {

  render() {
    return (
      <View style={styles.tileStyle}>
              <View style={{width: 20, height: 20,}}>
              <Tile

                featured
                caption="Text"
              />
              </View>
              <View style={{width: 20, height: 20,}}>
              <Tile

                featured
                caption="Some Caption Text"
              />
              </View>
              <View style={{width: 20, height: 20,}}>
              <Tile

                featured
                caption="Some"
              />
              </View>

              <View style={{width: 20, height: 20,}}>
              <Tile

                featured
                caption="Text"
              />
              </View>
              <View style={{width: 20, height: 20,}}>
              <Tile

                featured
                caption="Some Caption Text"
              />
              </View>
              <View style={{width: 20, height: 20,}}>
              <Tile

                featured
                caption="Some"
              />
              </View>
            </View>
    );
  }
}

const styles = StyleSheet.create({
  tileStyle: {
    flex: 1,
    flexDirection: 'row',

    // alignItems: 'center',

    // justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
  }
});

我只能按列获得三个图块,并且没有它们的宽度或高度。 请帮忙解决。

【问题讨论】:

  • 这就是你要找的东西codesandbox.io/s/j4y0pnr3p5
  • @AravindS 是这样的。但我尝试为 Tiles 使用类似的概念,但没有成功。
  • 而不是&lt;Text&gt; 将其替换为&lt;Tile&gt; 并查看

标签: react-native flexbox


【解决方案1】:

使用flex-wrap。在构造函数中获取窗口的宽度,并在Tilewidth 属性中使用它来获得您想要的比例宽度。

import {
  View,
  Dimensions,
  StyleSheet
} from "react-native";
import { Tile } from 'react-native-elements';

export class Screen extends Component {
  constructor() {
    super()

    this.state = {
      width: Dimensions.get('window').width
    }
  }

  render() {
  const { width } = this.state;
    return (
      <View style={{
        flex: 1,
        flexWrap: 'wrap',
        flexDirection: 'row',
        backgroundColor: '#ecf0f1'
      }}>
        <Tile
          width={width/3}
          featured
          caption="1"
        />
        <Tile
          width={width/3}
          featured
          caption="2"
        />
        <Tile
          width={width/3}
          featured
          caption="3"
        />
        <Tile
          width={width/3}
          featured  
          caption="4"
        />
      </View>
    );
  }
}

【讨论】:

  • 此解决方案是专门针对 Tile 组件还是适用于任何组件?
  • flex-wrap 是实现你想要的一般方法,考虑到 react-native 就是关于 flexbox 的。我可能会将width 设置为例如33% 而不是做Dimensions.get(不知道我在想什么)。这就是说你通常没有width-prop 可以使用,所以这是特定于Tiles 的,所以如果你使用Views,你可以在css中设置宽度。
猜你喜欢
  • 2020-10-23
  • 2019-10-28
  • 2021-09-20
  • 2019-08-14
  • 2015-06-02
  • 2015-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多