由于 FlatList 作为其子视图,超级视图的高度设置不正确

Height of super view is not setting properly because of FlatList as its subview

我试图在视图中创建一个 FlatList,但是当我将视图的位置设置为 Absolute 时,它​​占用了 FlatList 的总内容大小。但是我想将视图的高度保持为导航栏和底部栏之间的剩余屏幕尺寸,但我不知道如何实现。

这是我的代码:

import React from 'react';

import { View, StyleSheet, SafeAreaView, FlatList, Image, } from 'react-native';
import { DARK_GREY_COLOR_CODE, GREY_COLOR_CODE, MAGENTA_COLOR_CODE } from '../Constant/Constants';


export default class ExplorePage extends React.Component {
  _renderTutorialList() {
  }

  render() {
    const sampleNameArray = [
      {
        id: 'bd7acbea-c1b1-46c2-aed5-3ad53abb28ba',
        title: 'Kinky',
      },
      {
        id: '3ac68afc-c605-48d3-a4f8-fbd91aa97f63',
        title: 'Waves/Loose Curls',
      },
      {
        id: '58694a0f-3da1-471f-bd96-145571e29d72',
        title: 'Curly',
        image_name: './Images/hairtype_thum_image.png',
      },
      {
        id: '58694a0f-3da1-471f-bd96-145571e29d79',
        title: 'Coily',
      },
    ];

    return (
      <SafeAreaView style={{flex:1, }}>
        <View style={{ flex: 1, position: 'absolute', width: '100%', backgroundColor: GREY_COLOR_CODE }}>
          <FlatList
            data={sampleNameArray}
            renderItem={({ item }) =>
              <View style={{width: '89.3%', height: 302, marginLeft: '5.4%', marginRight: '5.4%', }}>
                <Image source={require('../Images/user_icon.png')} style={{ position: 'absolute', width: '100%', height: '90%', borderRadius: 10, backgroundColor: MAGENTA_COLOR_CODE}} />
              </View>
            }
            keyExtractor={item => item.id}
          />
        </View>
      </SafeAreaView>
    );
  }
}

const styles = StyleSheet.create({
  button: {
    alignItems: 'center',
    backgroundColor: '#DDDDDD',
    padding: 10,
    width: 300,
    marginTop: 16,
  },
});

请帮帮我。我无法修复它,因为我是 React Native 的新手。

截图如下:

试试这个

首先渲染 Flatlist 中的项目和 return Flatlist 项目的视图

 render() {
return (
  <View style={{flex: 1}}>
    <FlatList
      extraData={this.state}
      data={sampleNameArray}
      keyExtractor={item => {
        return item;
      }}
      renderItem={this.renderItem}
    />
  </View>
);

这是您的主要渲染方法,然后在下一次渲染中渲染项目并调整您的列表样式,并按照描述使用样式 style={styles.stylename}

 renderItem = ({item}) => {
return (
  <View>
    <View style={styles.body}>
      <Text style={styles.text}> {item.data1}</Text>
      <Text style={styles.text}> {item.data2}</Text>
      <Text style={styles.text}> {item.data3}</Text>
    </View>
  </View>
);

你可以设置你想要如何渲染你的项目,就像我喜欢的那样 然后你可以为每个元素设置 CSS 属性。

希望它对你有用