【问题标题】:React Native elevation StyleSheet not working in FlatListReact Native 高程样式表在 FlatList 中不起作用
【发布时间】:2019-06-23 18:13:30
【问题描述】:

我正在尝试在 FlatList 中设置我的 renderItem 样式,但海拔不能正常工作。有什么我错了还是这是 React Native 问题?

我也测试了 ListView 但它仍然无法正常工作

这是 TodoItem 组件

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

const styles = StyleSheet.create({
    container: {
        height: 60,
        backgroundColor: 'white',
        borderRadius: 10,
        shadowColor: '#000',
        shadowOffset: { width: 2, height: 2 },
        shadowOpacity: 0.4,
        shadowRadius: 2,
        elevation: 3,
        justifyContent: 'center',
        paddingHorizontal: 30,
        marginBottom: 12
    },
    text: {
        fontSize: 18,
        fontWeight: '400',
        color: '#080808'
    }
})

export default class TodoItem extends Component {
    render() {
        return (
            <View style={styles.container}>
                <Text style={styles.text}> {this.props.text} </Text>
            </View>
        )
    }
}

这就是我在 FlatList 中调用它的地方

<FlatList
    data={this.props.items}
    renderItem={(item) => <TodoItem key={item.index} text={item.item} />}
/>

关键是,如果我不使用这样的 FlatList,海拔可以正常工作

<TodoItem text="Hello world" />

What I excepted What I see

【问题讨论】:

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


    【解决方案1】:

    有一个边距属性给我带来了问题,所以更改了属性并将其放在最后。

    第一个代码

    const styles = StyleSheet.create({
    container: {
    flex: 1,
    backgroundColor: '#fff',}, 
    content:{
    flex:1,
    padding: 40,
    backgroundColor:'pink',
    },
    list: {
    marginTop: 20,
    flex:1,
    backgroundColor:'yellow',
    },});
    

    新代码

    const styles = StyleSheet.create({
    container: {
    flex: 1,
    backgroundColor: '#fff',}, 
    content:{
    flex:1,
    padding: 40,
    backgroundColor:'pink',
    },
    list: {
    
    flex:1,
    backgroundColor:'yellow',
    marginTop: 20,
    },});
    

    如果有人能澄清它为什么会发生。

    【讨论】:

      【解决方案2】:

      大多数此类问题是由应用于您的周围视图或您尝试呈现的行的样式引起的。

      如果您将marginHorizontal: 10 添加到styles.container 的行中,则可能应该为您执行此操作。

      这是一个简化的示例,其中行的边缘没有被切断。它进行了一些调整以使其工作,使用state.items 而不是props.items 并将TodoItem 的样式名称更改为itemContainer。它应该让您了解如何实现它。

      import * as React from 'react';
      import { Text, View, StyleSheet, FlatList } from 'react-native';
      import { Constants } from 'expo';
      
      export default class App extends React.Component {
      
        state = {
          items: [
            'Hello'
          ]
        }
      
        render() {
          return (
            <View style={styles.container}>
              <FlatList
                data={this.state.items}
                renderItem={(item) => <TodoItem key={item.index} text={item.item} />}
              />
            </View>
          );
        }
      }
      
      class TodoItem extends React.Component {
          render() {
              return (
                  <View style={styles.itemContainer}>
                      <Text style={styles.text}> {this.props.text} </Text>
                  </View>
              )
          }
      }
      
      const styles = StyleSheet.create({
        container: {
          flex: 1,
          justifyContent: 'center',
          paddingTop: Constants.statusBarHeight + 10,
          backgroundColor: '#ecf0f1',
        },
        itemContainer: {
          marginHorizontal: 10,
          height: 60,
          backgroundColor: 'white',
          borderRadius: 10,
          shadowColor: '#000',
          shadowOffset: { width: 2, height: 2 },
          shadowOpacity: 0.4,
          shadowRadius: 2,
          elevation: 3,
          justifyContent: 'center',
          paddingHorizontal: 30,
          marginBottom: 12
        },
        text: {
          fontSize: 18,
          fontWeight: '400',
          color: '#080808'
        }
      });
      

      这是一个显示它工作的小吃https://snack.expo.io/@andypandy/flatlist-with-elevation-on-rows

      【讨论】:

        猜你喜欢
        • 2020-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-01
        • 2020-09-25
        • 2021-09-04
        • 2021-05-24
        • 2019-07-17
        相关资源
        最近更新 更多