简体   繁体   English

我如何在水平行中对齐 react-native 按钮

[英]How do i align react-native buttons in a horizontal row

I have 8 buttons which i would like to align in horizontal row, side by side.我有 8 个按钮,我想在水平行中并排对齐。 everything i have tried hasn't worked and the just display one on top of another.我尝试过的一切都没有奏效,只是将一个显示在另一个之上。

Here's what i have at the mo.这就是我在 mo 所拥有的。 Thanks谢谢

               categories: ['art', 'funny', 'inspire', 'life', 'love', 'management', 'sports', 'students'],



render() {
let cat = this.state.categories.map((value, key) => {
                return (
                     <View style={styles.catButtons}>
                          <TouchableOpacity
                               style={styles.buttonContainer}
                               key={key}
                               activeOpacity={0.7}
                          >
                               <Button style={styles.button}
                                    color={'darkslategrey'}
                                    key={key}
                                    title={value}
                                    onPress={() => { this.getCategoryQuote(value) }}
                               />
                          </TouchableOpacity>
                     </View>
                )
           })
           return (
                <View>
                     {cat}
                </View>
           )
}
const styles = StyleSheet.create({

 catButtons: {
      flexWrap: 'wrap'
 },
 buttonContainer: {
         width: 125,
          margin: 10
     }, 
});

I think, in this case, the flex items are the buttons.我认为,在这种情况下,弹性项目是按钮。 But the problems lies in the flex container: TouchableOpacity.但问题在于 flex 容器:TouchableOpacity。
As your code suggest, there is one flex container for one button, and that is repeated by the map function.正如您的代码所建议的那样,一个按钮有一个弹性容器,map function 重复了这一点。
It should be one flex container for all flex items (buttons).它应该是所有弹性项目(按钮)的一个弹性容器。 That means, the flex container should be outside of your map function and the items directly as its children (that are mapped by the map function).这意味着,弹性容器应该在您的 map function 和直接作为其子项的项目之外(由 map 函数映射)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM