【问题标题】:How to render two items side by side ReactNative如何并排渲染两个项目 React Native
【发布时间】:2019-10-25 12:07:42
【问题描述】:

假设我在一个内部有 <ListingCard/> 组件 <ScrollView/> 。 我想要的是在容器类上并排渲染那些<ListingCard/>

这是我迄今为止尝试过的:

<ListingCard/>

const ListingCard = (props) => {
    return (
        <View style={styles.container}>
          <Text>This is ListingCard Component</Text>
        </View>
    );
};

const styles = StyleSheet.create({
    container: {
      //flex: 1,
      alignItems: 'center',
      justifyContent: 'center',
      height: 150,
      width: Dimensions.get('window').width / 2 - 6,
      backgroundColor: colors.WHITE,
      borderRadius: 5,
      marginHorizontal:10,
      marginBottom: 10
    },
  });


export default ListingCard;

这是我使用&lt;ListingCard/&gt;的方式:

  render() {
        const { currentCategory } = this.state;
        return (
            <Drawer
                ref={(ref) => this._drawer = ref}
                type="static"
                onOpenStart={() => {
                    this.setState({
                        isDrawerOpen: true,
                    })
                }}
                onClose={() => {
                    this.setState({
                        isDrawerOpen: false,
                    })
                }}
                content={<SideFilterMenu />}
                tapToClose={true}
                side={'right'}
                openDrawerOffset={0.2} // 20% gap on the right side of drawer
                panCloseMask={0.2}
                closedDrawerOffset={-3}
            >
                <View style={styles.container}>
                    <CustomHeader
                        onPress={() => this.handleFilterPress()}
                        headerText={currentCategory && currentCategory.categoryName}
                        isIconVisible={true}
                        rightButtonText={'Filtrele'}
                        onIconPress={() => this.handleBackPress()}
                    />
                    <ScrollView 
                    style={{flex:1}}
                    contentContainerStyle={styles.cardContainer}
                    >
                            <ListingCard />
                            <ListingCard />
                            <ListingCard />
                            <ListingCard />
                            <ListingCard />
                            <ListingCard />
                            <ListingCard />
                            <ListingCard />
                        </ScrollView>
                </View>
            </Drawer>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        //alignItems: 'center',
        //justifyContent: 'center',
        backgroundColor: colors.GENERAL_BCK,
        //paddingHorizontal: 5

    },
    cardContainer: {
        flexDirection: 'row',
        flexWrap: 'wrap'
    }
});

我尝试过的是,有或没有&lt;View&gt; 容器,但这根本没有帮助。我无法完成它的原因是我在 ReactNative 上相对较新,并且对这些样式感到困惑。 我无法完成并排渲染这些&lt;ListingCard/&gt;。任何帮助将不胜感激,谢谢。

【问题讨论】:

    标签: javascript reactjs react-native mobile stylesheet


    【解决方案1】:

    好的,我已经解决了。

    问题是&lt;ListingCard/&gt; 的宽度。

    它是:width: Dimensions.get('window').width / 2 - 6,,它太宽了,无法将其中两个放在同一行。所以我把它改成了:width: Dimensions.get('window').width / 2 - 20,,瞧!成功了。

    【讨论】:

      猜你喜欢
      • 2016-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      • 2019-02-15
      • 2019-04-06
      相关资源
      最近更新 更多