简体   繁体   English

如何将TextInput在页面上居中放置在react-native中?

[英]How to centered TextInput on page in react-native?

When TextInput focused and android keyboard opened need to centered TextInput on screen above keyboard. 当TextInput聚焦并且android键盘打开时,需要将TextInput居中放置在键盘上方的屏幕上。 Ho to make it? 何能做到?

For some reason justifyContent is not working for TextInput so just use margins. 由于某些原因,justifyContent不适用于TextInput,因此只需使用边距即可。 Here is example: 这是示例:

var styles = StyleSheet.create({
  textInput: {
    marginLeft: 50,
    marginRight: 50
  }
});

Just make sure to set styles to TextInput: 只要确保将样式设置为TextInput即可:

<TextInput style={styles.textInput} />

Almost forgot - do not set width, use margins to define size, or it won't work. 差点忘了-不要设置宽度,使用边距定义尺寸,否则将无法使用。

You'll need to use flexbox. 您需要使用flexbox。

On the parent container you'll need something like: 在父容器上,您将需要以下内容:

var styles = StyleSheet.create({
    contaner: {
      display: 'flex',
      flexDirection: 'row',
      justifyContent: 'center
    }
});

Here is a good guide to flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/ 这是有关flexbox的好指南: https ://css-tricks.com/snippets/css/a-guide-to-flexbox/

I know this is an old question, but I just had the same issue, and found a solution, so I thought I'd share for anyone else having this problem. 我知道这是一个老问题,但是我遇到了同样的问题,并找到了解决方案,所以我想与其他遇到此问题的人分享一下。

I have a container with styles: 我有一个带有样式的容器:

flex:1,
justifyContent: 'center',
alignItems: 'center'

Within that container I have several Text elements, along with a Slider and TextInput . 在那个容器中,我有几个Text元素,以及一个SliderTextInput Everything was centered properly except the TextInput . 除了TextInput之外,所有内容都正确居中。 To solve this, I added the following to the styles for my TextInput : 为了解决这个问题,我在TextInput的样式中添加了以下内容:

alignSelf: 'center'

And now the TextInput is properly centered on the screen. 现在, TextInput正确地在屏幕上居中了。

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

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