【问题标题】:React Native : Arranging elementsReact Native:排列元素
【发布时间】:2017-11-14 08:19:43
【问题描述】:

我正在构建一个非常简单的应用,其中包含一个选择器和两个输入/标签。

目前在我的 iphone 中看起来像这样。

这是我的代码

import React from 'react';
import { StyleSheet, Text, View, Button, Modal, TextInput, Picker } from 'react-native';

export default class App extends React.Component {

constructor(props) {
  super(props);

}



state = {
  b1text: 'Kg',
  b2text: 'Cm',
  weight: '',
  height: '',
  standard: 'Metric'
}

render() {
  return (

    <View style={styles.container}>
    <Picker
            selectedValue={this.state.standard}
            onValueChange={(itemValue, itemIndex) => {
                                                        this.setState({standard: itemValue});
                                                        if(itemValue === "Metric") {
                                                        this.setState({b1text: "Kg"});
                                                        this.setState({b2text: "Cm"});
                                                        }
                                                        if(itemValue === "Imperial") {
                                                          this.setState({b1text: "Lbs"});
                                                          this.setState({b2text: "Inches"});
                                                        }

                                                    } }
            style={{height: 100, width: 100 }}

        >
            <Picker.Item label="Metric" value="Metric" />
            <Picker.Item label="Imperial" value="Imperial" />
    </Picker>

    <TextInput
              style={{height: 40, width: 60, borderColor: 'gray', borderWidth: 1}}
            onChangeText={(text) => this.setState({text: weight})}
            value={this.state.weight}
          />
    <Text>{this.state.b1text}</Text>
    <TextInput
              style={{height: 40, width: 60, borderColor: 'gray', borderWidth: 1}}
            onChangeText={(text) => this.setState({text: height})}
            value={this.state.height}
          />

    <Text>{this.state.b2text}</Text>


    </View>

  );

}

}

const styles = StyleSheet.create({
  container: {
      flex: 1,
      backgroundColor: '#fff',
      alignItems: 'center',
      justifyContent: 'center',
      flexDirection: 'row',


  },
});

但我希望它看起来像这样,如下所示。

我尝试了边距、填充等。仍然没有运气。

谁能告诉我我可以使用什么 css/flex 属性来改变 UI,就像我想要的那样?

【问题讨论】:

    标签: react-native flexbox expo react-native-flexbox


    【解决方案1】:

    我创建了一个 Expo Snack,其中包含您想要实现的 UI 的更接近示例。但我会留给你解决细节。

    import React from 'react';
    import { StyleSheet, Text, View, TextInput, Picker } from 'react-native';
    
    export default class App extends React.Component {
      constructor(props) {
        super(props);
      }
    
      state = {
        b1text: 'Kg',
        b2text: 'Cm',
        weight: '',
        height: '',
        standard: 'Metric',
      };
    
      render() {
        return (
          <View style={styles.container}>
            <View style={styles.top}>
              <Picker
                selectedValue={this.state.standard}
                onValueChange={itemValue => {
                  this.setState({ standard: itemValue });
                  if (itemValue === 'Metric') {
                    this.setState({ b1text: 'Kg' });
                    this.setState({ b2text: 'Cm' });
                  }
                  if (itemValue === 'Imperial') {
                    this.setState({ b1text: 'Lbs' });
                    this.setState({ b2text: 'Inches' });
                  }
                }}>
                <Picker.Item label="Metric" value="Metric" />
                <Picker.Item label="Imperial" value="Imperial" />
              </Picker>
            </View>
            <View style={styles.bottom}>
              <TextInput
                style={{
                  height: 40,
                  width: 60,
                  borderColor: 'gray',
                  borderWidth: 1,
                }}
                onChangeText={() => this.setState({ text: weight })}
                value={this.state.weight}
              />
              <Text>{this.state.b1text}</Text>
              <TextInput
                style={{
                  height: 40,
                  width: 60,
                  borderColor: 'gray',
                  borderWidth: 1,
                }}
                onChangeText={() => this.setState({ text: height })}
                value={this.state.height}
              />
              <Text>{this.state.b2text}</Text>
            </View>
          </View>
        );
      }
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
      },
      top: {
        width: '100%',
        flex: 1,
      },
      bottom: {
        flex: 1,
        alignItems: 'center',
      },
    });
    

    您需要做的重要事情之一是学习如何使用react-native 编写样式。这是一个资源,其中包含您可以与 const {StyleSheet} from 'react-native'. 一起使用的所有样式属性的指南

    https://github.com/vhpoet/react-native-styling-cheat-sheet

    祝你好运:)

    【讨论】:

    • 您需要在此处发布您的代码,而不是任何第三方网站,明天可能会更改或消失,从而使您的答案无用。 stackoverflow.com/help/how-to-answer
    • 感谢您提供这些指南,我已经进行了必要的调整以在此处提供代码。我很抱歉,因为当我们提供支持时,我们通常会在需要提供代码解决方案时链接 snap.expo.io。
    猜你喜欢
    • 2021-07-24
    • 2021-10-25
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 2018-07-18
    • 2018-02-07
    • 2017-08-11
    • 2020-01-26
    相关资源
    最近更新 更多