FlatList组件无法设置样式

时间:2019-11-22 11:56:44

标签: react-native

我正在尝试设置ListItem组件的背景色:

<FlatList
  data={this.props.search.videos}
  renderItem={({ item }) => (
    <View style={{ backgroundColor: "red" }}>
      <ListItem
        title={item.title}
        onPress={() => this.onPress(item.id)}
      />
    </View>
  )}
  contentContainerStyle={{
    backgroundColor: "red",
    overflow: "hidden",
    backgroundColor: "#00336690"
  }}
  keyExtractor={item => item.id.toString()}
  ItemSeparatorComponent={this.renderSeparator}
/>

backgroundColor始终显示为白色,如何更改?

ListItem:

<ListItem
  underlayColor="red"
  style={{ backgroundColor: "#000" }}
  title={item.title}
  onPress={() => this.onPress(item.id)}
/>

2 个答案:

答案 0 :(得分:0)

我认为您需要在ListItem本身上设置样式。

 <FlatList
    data={this.props.search.videos}
    renderItem={({ item }) => (
                    <View>
                        <ListItem
                            title={item.title}
                            onPress={() => this.onPress(item.id)}
                            style={{ backgroundColor: "red" }}
                        />
                    </View>
                )}
                contentContainerStyle={{
                    backgroundColor: "red",
                    overflow: "hidden",
                    backgroundColor: "#00336690"
                }}
                keyExtractor={item => item.id.toString()}
                ItemSeparatorComponent={this.renderSeparator}
            />

答案 1 :(得分:0)

尝试从

删除一种backgroundColor
contentContainerStyle={{
                backgroundColor: "red",
                overflow: "hidden",
                backgroundColor: "#00336690"
            }}

像这样

<FlatList
                contentContainerStyle={{
                    backgroundColor: "red",
                    overflow: "hidden",
                }}
                data={this.props.search.videos}
                renderItem={({ item }) => (
                    <View>
                        <ListItem 
                            style={{ backgroundColor: "red" } }
                            title={item.title}
                            onPress={() => this.onPress(item.id)}
                        />
                    </View>
                )}
                keyExtractor={item => item.id.toString()}
                ItemSeparatorComponent={this.renderSeparator}
            />