【问题标题】:React Native: Blank space between FlatList rendered itemsReact Native:FlatList渲染项目之间的空白
【发布时间】:2019-08-27 13:48:59
【问题描述】:

我正在尝试使用 FlatList 在应用程序中显示一些获取的数据。它可以工作,但项目之间有一个非常大的空间!

这是我的 FlatList 代码:

<View style={styles.showresp}>
                <FlatList
                    data={this.state.responsjson}
                    renderItem={({ item }) =>
                    <View style={styles.weatherview}>
                            <Text style={styles.city}>{item.name}</Text>
                            <Text style={styles.country}>{item.country}</Text>
                            <Text style={styles.temp}>{item.temp_c}</Text> 
                    </View>}/>
</View>

this is what i see in screen

它是样式:

showresp: {
    backgroundColor: '#fffa',
    height: 315,
    marginRight: '10%',
    marginLeft: '10%',
    marginTop: '15%',
    borderRadius: 15
},
 weatherview:{
        alignItems: 'center',
        justifyContent: 'center',
        flex :1
},
city: {
    fontFamily :'Wonderbar Demo',
    fontSize:40,
    color:'#880e4f',

},
country:{
    fontSize:20,
    fontFamily:'Samim-Bold',
    backgroundColor:'red',

},
temp:{
    fontFamily :'Wonderbar Demo',
    fontSize : 40,
    backgroundColor:'green',

},

我设置了上下文本的背景颜色以找到问题,但我对此一无所知。

你能在这件事上指导我吗?ಠ_ಠ

【问题讨论】:

  • 您是否在 style={styles.weatherview} 中添加了“flex:1”。请删除然后检查
  • 是的,我试过了,但没用:/
  • 你能展示你的样式文件吗?
  • 我将它们添加到问题中
  • 我试过你的代码,没有显示空间,“styles.showresp”的样式是什么。请补充问题

标签: android react-native react-native-android react-native-flatlist


【解决方案1】:

我为你做了一个例子。看这个。你和我的区别是没有边际的。

我的父母是flatist

您不必像这样将 View 放在一边。您可以将视图放在您想要的项目中。

import React, { Component } from 'react';
import { View, Text, FlatList } from 'react-native';

const users = [
  {
    name: 'Cathy Gilliam',
    company: 'EXOVENT',
    email: 'cathygilliam@exovent.com',
  },
  {
    name: 'Norton Potts',
    company: 'COREPAN',
    email: 'nortonpotts@corepan.com',
  },
  {
    name: 'Kelly Collins',
    company: 'SECURIA',
    email: 'kellycollins@securia.com',
  },
];

export default class App extends Component {
  render() {
    return (
      <FlatList
        data={users}
        renderItem={({ item }) => (
          <View
            style={{
              borderBottomWidth: 1,
              borderBottomColor: 'grey',
              padding: 10
            }}>
            <View>
              <Text style={{ fontWeight: 'bold', fontSize: 18 }}>{item.name}</Text>
              <Text>{item.company}</Text>
              <Text>{item.email}</Text>
            </View>
          </View>
        )}
      />
    );
  }
}

【讨论】:

  • 我像您的示例一样编辑了我的代码,但是,我得到了相同的结果:/。是否可能与API相关? ¯_(ツ)_/¯
【解决方案2】:

它应该可以工作:

import * as React from 'react';
import { Text, View, StyleSheet, FlatList } from 'react-native';
import Constants from 'expo-constants';

// You can import from local files
import AssetExample from './components/AssetExample';

// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      responsjson: [{
        name: 'Name1',
        country: 'Country1',
        temp_c: '45'
      },
      {
        name: 'Name2',
        country: 'Country2',
        temp_c: '45'
      }]
    };
  }

  render() {
    return (
      <View style={styles.showresp}>
        <FlatList
          data={this.state.responsjson}
          renderItem={({ item }) =>
            <View style={styles.weatherview}>
              <Text style={styles.city}>{item.name}</Text>
              <Text style={styles.country}>{item.country}</Text>
              <Text style={styles.temp}>{item.temp_c}</Text>
            </View>} />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  showresp: {
    backgroundColor: '#fffa',
    height: 315,
    marginRight: '10%',
    marginLeft: '10%',
    marginTop: '15%',
    borderRadius: 15
  },
  weatherview: {
    alignItems: 'center',
    justifyContent: 'center',
    flex: 1
  },
  city: {
    fontFamily: 'Wonderbar Demo',
    fontSize: 40,
    color: '#880e4f',

  },
  country: {
    fontSize: 20,
    fontFamily: 'Samim-Bold',
    backgroundColor: 'red',

  },
  temp: {
    fontFamily: 'Wonderbar Demo',
    fontSize: 40,
    backgroundColor: 'green',

  },
});

图片:

【讨论】:

  • 是的,它可以工作,但是当我尝试在我的应用程序中使用它时,我得到相同的结果:/。是否可能与API相关?我可以请你检查一下这个 api:“api.apixu.com/v1/…
猜你喜欢
  • 2020-12-15
  • 2017-06-21
  • 1970-01-01
  • 2021-09-05
  • 2022-01-23
  • 1970-01-01
  • 2018-03-21
  • 2021-03-21
相关资源
最近更新 更多