繁体   English   中英

带有滚动视图的 Flex 按钮 React Native

[英]Flex buttons with scrollview React Native

我已经上下搜索,试图为我遇到的这个问题找到答案。 基本上,我有一个滚动视图,其中包含许多复选框。 我想在底部有几个按钮,它们是“全选”“全选”类型的操作。 我希望按钮平等地占用可用空间(例如拉伸以便没有间隙)。 我为按钮和容器尝试了大量不同的样式组合,但都没有成功。

我在这里有一个工作示例,但为了方便起见,我也会发布代码。 任何帮助或指导将不胜感激。

编辑:正如建议的那样,我从react-native-elements查看了ButtonGroup ,但我不喜欢按钮如何保持选中状态。 另外,我觉得这是一个常见的问题,我还没有找到解决方案,在 React Native 中使用 flexbox。

在此处输入图片说明

https://snack.expo.io/BJNEmvMvQ

import React, { Component } from 'react';
import { View, Text, ScrollView } from 'react-native';
import { CheckBox, ListItem, Button } from 'react-native-elements';

export default class App extends Component {
  list = [ list of objects to render checkboxes (see snack for example  ];

  renderItem = (item, i) => {
    return (
      <View key={i}>
        <CheckBox
          title={item.Description}
          checkedIcon="check"
          uncheckedIcon="times"
        />
      </View>
    )
  }
  render() {
    return (
      <View style={{ flex: 1 }}>
        <ScrollView>{this.list.map(this.renderItem)}</ScrollView>
        <View style={{ flexDirection: 'row',
              justifyContent: 'center' }}>
        // have tried in the view style above: flexGrow, alignItems, and others
          <Button
            title="hello"
            containerViewStyle={{ borderWidth: 2,
            borderColor: 'black'}}
            // have tried using flexGrow, alignSelf  
            // have also tried using 'buttonStyle' here instead of 'containerViewStyle'
          />
          <Button
            title="hello"
            containerViewStyle={{ borderWidth: 2,
            borderColor: 'black' }}
          />
          <Button
            title="hello"
            containerViewStyle={{ borderWidth: 2,
            borderColor: 'black' }}
          />
          <Button
            title="hello"
            containerViewStyle={{ borderWidth: 2,
            borderColor: 'black' }}
          />
        </View>
      </View>
    );
  }
}

更改 React Native 按钮样式是有限制的。 您的问题的快速解决方案是用 View 包装 Button。

这是工作演示: https : //snack.expo.io/SkIXThMw7

    <View style={{ flex: 1 }}>
            <ScrollView>{this.list.map(this.renderItem)}</ScrollView>
            <View style={{flexDirection: 'row'}}>
                <View style={{flex:1}} >
                    <Button
                        title="hello"
                        containerViewStyle={styles.buttonStyle}
                    >
                    </Button>
                </View>
                <View style={{flex:1}} >
                    <Button
                        title="hello"
                        containerViewStyle={styles.buttonStyle}
                    >
                    </Button>
                </View>
                <View style={{flex:1}} >
                    <Button
                        title="hello"
                        containerViewStyle={styles.buttonStyle}
                    >
                    </Button>
                </View>
                <View style={{flex:1}} >
                    <Button
                        title="hello"
                        containerViewStyle={styles.buttonStyle}
                    >
                    </Button>
                </View>
            </View>
    </View>

const styles = StyleSheet.create({
    buttonStyle: {
      borderWidth: 1,
      borderColor: 'black',
      marginLeft: 0,
      marginRight: 0,
      paddingLeft: 0,
      paddingRight: 0
   },

});

react-native-elements帮助下,我能够解决这个问题。 我需要从containerViewStyle上默认的按钮中删除边距,并添加一个flex: 1 更新的小吃在这里 它与其他答案之一基本相同,只是您不必将按钮包装在视图中,只需将样式应用于每个按钮的containerViewStyle

import React, { Component } from 'react';
import { View, Text, ScrollView } from 'react-native';
import { CheckBox, ListItem, Button } from 'react-native-elements';

export default class App extends Component {
  list = [ list of objects to render checkboxes (see snack for example  ];

  renderItem = (item, i) => {
    return (
      <View key={i}>
        <CheckBox
          title={item.Description}
          checkedIcon="check"
          uncheckedIcon="times"
        />
      </View>
    )
  }
  render() {
    return (
      <View style={{ flex: 1 }}>
        <ScrollView>{this.list.map(this.renderItem)}</ScrollView>
        <View style={{ flexDirection: 'row', justifyContent: 'center' }}>
          <Button
            title="hello"
            containerViewStyle={{ borderWidth: 2, borderColor: 'black', marginLeft: 0, marginRight: 0, flex: 1 }}
          />
          <Button
            title="hello"
            containerViewStyle={{ borderWidth: 2, borderColor: 'black', marginLeft: 0, marginRight: 0, flex: 1 }}
          />
          <Button
            title="hello"
            containerViewStyle={{ borderWidth: 2, borderColor: 'black', marginLeft: 0, marginRight: 0, flex: 1 }}
          />
          <Button
            title="hello"
            containerViewStyle={{ borderWidth: 2, borderColor: 'black', marginLeft: 0, marginRight: 0, flex: 1 }}
          />
        </View>
      </View>
    );
  }
}

我希望尝试使用 ButtonGroup 是有用的,它是 react-native-elements 的一部分,并在零食示例中对渲染函数进行以下更改:

上述解决方案的参考是https://snack.expo.io/Hkdg7OGv7在这里给出请检查。

暂无
暂无

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

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