【问题标题】:unable to move button to the right无法向右移动按钮
【发布时间】:2018-05-01 02:20:23
【问题描述】:

我有以下 return() 并且无法将包含按钮 title = 'done' 的最后一个 VIEW 移动到右侧

return (
      <View style={styles.container}>
        <Text style={styles.bigFont}>{`${this.state.timer + 'TIMER'}`}</Text>
        <Text style={styles.bigFont}>{`${this.state.min + ':' + this.state.sec}`}</Text>
        <View style={styles.button}>
          <Button title= {this.state.startFlag ? 'PAUSE' : 'START'} onPress={()=>this.startToggle()} />
          <Button title='RESET' onPress={()=>this.resetToggle()} />
        </View>
             <View style={styles.row}>
                <Text style={[styles.bold,{marginRight:10},{width:112},
                            {textAlign:'right'}]}>
                            Work Timer:</Text>
                <Text style={styles.bold}> min:</Text>
                <TextInput
                   value={Math.floor(this.state.workTime / 60).toString()}
                   style={styles.input}

                   onChangeText={(text)=>{this.captureInput(text)}}
                   onSubmitEditing={()=>{(this.update('work-min'))}}
                />
                <Text style={styles.bold}> sec:</Text>
                <TextInput
                   value={Math.floor(this.state.workTime % 60).toString()}
                   style={styles.input}
                   keyboardType='numeric'
                   onChangeText={(text) => {this.captureInput(text)}}
                   onSubmitEditing={()=>{(this.update('work-sec'))}}
                />
             </View>
             <View style={styles.row}>
                <Text style={[styles.bold,{marginRight:10},{width:112},
                            {textAlign:'right'}]}>
                            Break Timer:</Text>
                <Text style={styles.bold}> min:</Text>
                <TextInput
                   value={Math.floor(this.state.breakTime / 60).toString()}
                   style={styles.input}
                   keyboardType='numeric'
                   onChangeText={(text) => {this.captureInput(text)}}
                   onSubmitEditing={()=>{(this.update('break-min'))}}
                />
                <Text style={styles.bold}> sec:</Text>
                <TextInput
                   value={Math.floor(this.state.breakTime % 60).toString()}
                   style={styles.input}
                   keyboardType='numeric'
                   onChangeText={(text) => {this.captureInput(text)}}
                   onSubmitEditing={()=>{(this.update('break-sec'))}}
                />
             </View>
             <View style={{flexDirection:'row',justifyContent:'flex-end',alignItems:'flex-end'}} >
                <Text ><Button title='done' onPress={()=>this.resetToggle()} /></Text>
             </View>

      </View>

    )
export default StyleSheet.create({
    container: {
      flex:1,
      flexDirection:'column',
      backgroundColor: '#fff',
      alignItems: 'center',
      justifyContent: 'center',
      marginBottom:150,
    },
    bigFont:{
      fontSize:40,
      color:'darkblue'
    },
    row:{
      flexDirection:'row',
      marginBottom:15,
    },
    button:{
      flexDirection:'row',
      marginTop:30,
      marginBottom:30,
    },
    bold: {
      fontWeight: 'bold',
      color:'green',
    },
    input: {
      borderWidth: 1,
      borderColor: 'black',
      marginRight: 10,
      paddingHorizontal:15,
      minWidth: 50,
    },
  })

【问题讨论】:

  • 你能添加container的样式吗?

标签: react-native flexbox


【解决方案1】:

所以,有趣的是,我们需要使用 alignSelf:'flex-end' 或使用 width:'100%'。 flex:1 绝对是错误的,因为它使用了所有可用空间并将所有内容向上移动。

【讨论】:

    【解决方案2】:

    下面的代码应该可以工作。 return 语句的最后一部分。

    <View style={{flex: 1, flexDirection: 'row', justifyContent: 'flex-end', alignItems: 'flex-end'}} >
    <Text ><Button title='done' onPress={() => this.resetToggle()} /></Text>
    

    【讨论】:

    • 我认为 flex:1 也可以,但它最终将所有内容都向上移动并仍然将按钮保持在中心。我将容器的样式添加到帖子中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多