FlatList 中标题和正文的分隔符样式

nin*_*yen 5 react-native react-native-flatlist

目前,我在使用 FlatList 时遇到了问题。我有一个组件来呈现列表簿。按照设计,header 的宽度是屏幕的宽度,body 将左右填充 10px。

所以我使用了contentContainerStyle={{paddingHorizontal: 10}}. 但是结果是header和body都是10px左右填充。

请提出解决方法。对不起,我的英语不好!!

更新:我很抱歉没有彻底描述我的问题。

main.tsx

...
public render() {
  return (
    <FlatList
      key...
      data={..}
      renderItem={this.renderItems}
      ListHeaderComponent={this.renderHeader}
      contentContainerStyle={styles.contentStyle}
    />
  );
}

private renderHeader = () => {
  return (
    <View style={style.header}
      //TODO something ...
    </View>
  );
}

private renderItems: ListRenderItem<IBook> = ({ item: {bookId} }) => bookId ?
  (
    <BookGridCell
      title={...}
      image={...}
      //TODO more..
    />
  ) : <View style={styles.emptyBox} /> 
}
Run Code Online (Sandbox Code Playgroud)

renderItems,我调用了一个组件BookGridCell。在这个组件中,设置了书籍的设计。所以如果我直接在里面添加样式renderItems,每本书的左右边距都是10px,而不是整个正文。

contentContainerStyle 与 contenContainerStyle 一起使用

直接在里面添加样式renderItems 时不要使用 contentContainerStyle

Chi*_*rma -2

  1. 为您的身体赋予风格。

    style={styles.bodyContainer}
    
    Run Code Online (Sandbox Code Playgroud)

然后在样式表中添加属性。

    const styles = StyleSheet.create({
    bodyContainer: {
      paddingHorizontal: 10    
      },
Run Code Online (Sandbox Code Playgroud)

这是正确的方法或者

  1. 您可以直接在视图中添加填充。

    style={{ paddingHorizontal: 10 }}
    
    Run Code Online (Sandbox Code Playgroud)

  • 这将设置列表主体、页眉和页脚的样式。没有用。 (3认同)