简体   繁体   English

无法覆盖react-native的TextInput默认填充

[英]Can't override react-native's TextInput default padding

I have the following TextInput: 我有以下TextInput:

  _renderTextInput() {
    return (
      <TextInput
        onChangeText={TextInput => this.setState({TextInput})}
        style={{flex: 1, backgroundColor: "maroon", paddingVertical: 0}}
        autoFocus={true}
        multiline={true}
        underlineColorAndroid="transparent"
        returnKeyType="go"
      />
    )
  }

  render() {
    return(
      <View style={{flex: 1, backgroundColor: "#E0E0E0", paddingVertical: 0}}>
        {this._renderTextInput()}
      </View>
    )
  }

What I am trying to achieve is a full-screen distraction-free zone for the user to type his post. 我想要实现的是一个全屏无干扰区域,供用户输入他的帖子。

I know for sure that it's a padding issue but whatever I try I simply can not override it. 我确定这是一个填充问题,但无论我尝试什么,我根本无法覆盖它。 I tried padding:0 paddingTop:0 paddingVertical: 0 nothing seems to work... 我试过padding:0 paddingTop:0 paddingVertical: 0似乎没什么用......

Preview: 预习:

在此输入图像描述

After a long day of endless googling , I found out what's causing the unwanted padding. 经过漫长的一天无休止的谷歌搜索 ,我发现了什么导致不必要的填充。 It looks like by default, TextInput has the Text vertically centered hence why everything is exactly in the middle. 它看起来像默认情况下,TextInput的文本垂直居中,因此为什么一切都在中间。 To override it, just add: 要覆盖它,只需添加:

textAlignVertical: 'top' to the TextInput's style and you're done. textAlignVertical: 'top' TextInput风格的textAlignVertical: 'top' ,你已经完成了。 :) :)

@matt-aft 's comment about changing the style of the text helped to solve this. @ matt-aft关于改变文本风格的评论有助于解决这个问题。

Flex wont work here because you have other components also using flex so the screen is getting divided up. Flex不会在这里工作,因为你还有其他组件也使用flex,因此屏幕正在分割。 Using the device's height and width should work, or using a modal with a test input should also work. 使用设备的高度和宽度应该可以工作,或者使用带有测试输入的模态也应该有效。

const {height, width} = Dimensions.get('window');

style={{ height, width, backgroundColor: "maroon", paddingVertical: 0}}

In case anyone is looking for a solution while using an Input, set it's multiline property to true. 如果有人在使用输入时寻找解决方案,请将其多行属性设置为true。

multiline={true} 多= {TRUE}

       <TextInput multiline={true}
          underlineColorAndroid='transparent'
          style={styles.input}
          onChangeText={(text) => this.setState({ text })}
          value={this.state.text}
          placeholder={this.props.navigation.state.params.screen}
          placeholderTextColor={colors.light_grayc}
          textAlignVertical='top'
          paddingRight={12}
          paddingLeft={12}
        />

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

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