【问题标题】:How to adjust multiple checkboxes properly in react native?如何在本机反应中正确调整多个复选框?
【发布时间】:2019-06-11 13:28:59
【问题描述】:

我正在使用react-native-check-box 在我的应用中实现复选框。

假设我必须创建 4 个复选框并且它应该正确呈现。但是现在渲染了 4 个框,但它们的文本是上下移动的,而如果存在间距问题,剩余的框应该下降到下一行。或者它应该调整,但它的文本没有起伏。

请提出建议。

    []Facebook []Twitter []Whatsapp []Instagram

    []Facebook []Twitter 
    []Whatsapp []Instagram

    []Fac []Twi []Wha []Ins
      ebo   tte   tsa   tag
      ok    r     app   ram
       <View style={{
          flexDirection:'row', backgroundColor:'#fff', marginBottom:8
       }}>
       <CheckBox
         style={{flex: 1, padding: 1}}
         onClick={()=>{
           this.setState({
             isChecked:!this.state.isChecked
           })
         }}
         isChecked={true}
         rightText={"Facebook"}
       />

       <CheckBox
         style={{flex: 1, padding: 1}}
         onClick={()=>{
           this.setState({
             isChecked:!this.state.isChecked
           })
         }}
         isChecked={false}
         rightText={"Whatsapp"}
       />

       <CheckBox
         style={{flex: 1, padding: 1}}
         onClick={()=>{
           this.setState({
             isChecked:!this.state.isChecked
           })
         }}
         isChecked={this.state.isChecked}
         rightText={"Instagram"}
       />  
     </View>

谢谢。

【问题讨论】:

    标签: javascript reactjs react-native checkbox ecmascript-6


    【解决方案1】:

    我相信您可以使用flexWrap 进行布局,如here 所述。要使其适用于不同大小的视图,重要的是指定Checkbox 组件的固定宽度和View 组件的固定高度。

    【讨论】:

      【解决方案2】:

      检查这个-

         import * as React from 'react';
      import { Text, View, StyleSheet } from 'react-native';
      import CheckBox from 'react-native-check-box';
      
      export default class App extends React.Component {
        constructor() {
          super();
          this.state = {
            isChecked: false,
          };
        }
        render() {
          return (
            <View style={{ flex: 1, justifyContent: 'center' }}>
              <View
                style={{
                  width: '100%',
                  justifyContent: 'center',
                  alignItems: 'center',
                }}>
                <View
                  style={{
                    width: '100%',
                    flexDirection: 'row',
                    justifyContent: 'space-between',
                    alignItems: 'center',
                  }}>
                  <CheckBox
                    style={{ width: '50%', padding: 1 }}
                    rightTextStyle={{ marginLeft: 0, paddingLeft: 0 }}
                    onClick={() => {
                      this.setState({
                        isChecked: !this.state.isChecked,
                      });
                    }}
                    isChecked={true}
                    rightText={'Facebook'}
                  />
      
                  <CheckBox
                    style={{ width: '50%', padding: 1 }}
                    rightTextStyle={{ marginLeft: 0, paddingLeft: 0 }}
                    onClick={() => {
                      this.setState({
                        isChecked: !this.state.isChecked,
                      });
                    }}
                    isChecked={false}
                    rightText={'Whatsapp'}
                  />
                </View>
                <View
                  style={{
                    width: '100%',
                    flexDirection: 'row',
                    justifyContent: 'space-between',
                    alignItems: 'center',
                  }}>
                  <CheckBox
                    style={{ width: '50%', padding: 1 }}
                    rightTextStyle={{ marginLeft: 0, paddingLeft: 0 }}
                    onClick={() => {
                      this.setState({
                        isChecked: !this.state.isChecked,
                      });
                    }}
                    isChecked={this.state.isChecked}
                    rightText={'Instagram'}
                  />
      
                  <CheckBox
                    style={{
                      width: '50%',
                      padding: 1,
                    }}
                    rightTextStyle={{ marginLeft: 0, paddingLeft: 0 }}
                    onClick={() => {
                      this.setState({
                        isChecked: !this.state.isChecked,
                      });
                    }}
                    isChecked={this.state.isChecked}
                    rightText={'Twitter'}
                  />
                </View>
              </View>
            </View>
          );
        }
      }
      

      【讨论】:

      • 谢谢,但它再次出现上下 3 个字母,我正在尝试特定的东西不应该上下波动 .. 像 Facebook 所以所有字母都应该正确..下一个单词应该在下一行自动全词
      • 我对我的回答做了一些修改,请看一次
      猜你喜欢
      • 2021-12-30
      • 2022-11-09
      • 2018-08-15
      • 1970-01-01
      • 1970-01-01
      • 2017-08-20
      • 2023-03-25
      • 2019-11-24
      • 2021-11-09
      相关资源
      最近更新 更多