【问题标题】:FlatList list item background colorFlatList 列表项背景颜色
【发布时间】:2019-01-21 17:32:00
【问题描述】:

是否可以将 FlatList 的 backgroundColor 设置为透明?

我有 appbackground 图片,想将其用作屏幕背景。

但 FlatList 中的 ListItem 似乎需要设置一些颜色。 标题也是同样的问题。

    <FlatList
      style={styles.root}
      data={contacts}
      getItemLayout={this.getItemLayout}
      ItemSeparatorComponent={this.renderSeparator}
      keyExtractor={this.extractItemKey}
      renderItem={this.renderItem}
      enableEmptySections />

renderItem = ({ item }) => {
        return (
        <TouchableOpacity onPress={this.onPress}>
          <View style={styles.container}>
            <Avatar type='circle' style={styles.avatar} img={this.getAvatar(image, gender)} />
           <View style={{flexDirection: 'column'}}>
             <Text style={styles.text}>{`${lastName} ${firstName}`}</Text>
             <Text style={styles.phone}>{`${mobilePhone}`}</Text>
           </View>
          </View>
         </TouchableOpacity>
        );
    }

类似这里:

【问题讨论】:

  • 你能分享一些代码吗?喜欢:FlatlistrenderItem

标签: javascript reactjs react-native react-native-flatlist


【解决方案1】:

是的,只是不要在您的 FlatList 上设置 backgroundColor 并确保您的 Image 在 FlatList 下方。

如果这不起作用,请使用 rgba(255, 255, 255, 0.0) 设置颜色,这会将 alpha 设置为零,这意味着颜色是透明的。

https://facebook.github.io/react-native/docs/colors

这是一个快餐https://snack.expo.io/8nM1LgJqR,在FlatList 或行项目上没有设置backgroundColor

这里是代码

import React, {Component} from 'react';
import { View, StyleSheet, FlatList, Text, Image } from 'react-native';
export default class App extends React.Component {

  constructor(props) {
    super(props);

    this.state = {
      data: [
        {
          title: 'Card 1', city: 'London'
        },
        {
          title: 'Card 2', city: 'London 2'
        },
        {
          title: 'Card 3', city: 'London 3'
        },
        {
          title: 'Card 4', city: 'London 4'
        },
        {
          title: 'Card 5', city: 'London 5' 
        },
        {
          title: 'Card 6', city: 'London 6'
        },
        {
          title: 'Card 7', city: 'London 7'
        },
        {
          title: 'Card 8', city: 'London 8'
        },
        {
          title: 'Card 9', city: 'London 9'
        },
        {
          title: 'Card 10', city: 'London 10'
        },
      ]
    }
  }

    renderItem = ({ item }) => {
      return (
        <View style={{height: 100, borderWidth: 1, width: '100%'}}>
          <Text>{item.title}</Text>
        </View>

      );
    }
  
  render() {
    return (
      <View style={styles.container}>
      <Image style={{height: '100%', width: '100%', position:'absolute'}} source={{uri: 'https://i.stack.imgur.com/t96aT.jpg'}} />
      <FlatList
        style={{flex:1}}
        data={this.state.data}
        showsHorizontalScrollIndicator={false}
        keyExtractor={item => item.title}
        renderItem={this.renderItem}
      />
      </View>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  }
});

【讨论】:

  • 你使用的背景图片现在返回404。如果可以编辑答案以提供可用的图像,可能会对人们有所帮助
  • @PrashanD 已更新。谢谢你告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多