文本垂直对齐in react native(使用nativebase)

cki*_*m16 19 text vertical-alignment react-native native-base

嗨,我想知道有一种方法可以在React Native中垂直对齐.我试图在底部定位,但我认为我无法找到一个好方法.

如果有人知道如何解决这个问题,请告诉我.

谢谢,

Has*_*Eqx 45

要水平和垂直对齐任何内容,包括文本,只需在包含它的容器中使用以下内容

    container :{
       justifyContent: 'center', //Centered horizontally
       alignItems: 'center', //Centered vertically
       flex:1
    }
Run Code Online (Sandbox Code Playgroud)

  • 默认情况下,React Native 具有与 Web 相反的“flexDirection:column”。所以 justifyContent 将垂直居中 (6认同)
  • 评论似乎很混乱,对我来说,alignItems 垂直居中,justifyContent 水平居中。 (3认同)

小智 31

您可以使用该flex属性justifyContent来实现此目的.完整文档在这里.

<View style={{justifyContent: 'center'}}>
   <Text>Vertically Centered Text</Text>
</View>
Run Code Online (Sandbox Code Playgroud)


Rom*_*man 15

您可以使用仅限android的样式属性textAlignVertical

要在顶部对齐文字:

style={{textAlignVertical: 'top'}}
Run Code Online (Sandbox Code Playgroud)

要在中心对齐文字:

style={{textAlignVertical: 'center'}}
Run Code Online (Sandbox Code Playgroud)

要对齐底部的文字:

style={{textAlignVertical: 'bottom'}}
Run Code Online (Sandbox Code Playgroud)

  • textAlignVertical属性仅适用于Android. (9认同)
  • `textAlignVertical`样式是仅适用于Android的属性。如果您在iOS上进行开发,请尝试使用flexbox对齐属性`alignItems:center`。另请参见[textAlignVertical无效](/sf/ask/2447889391/)。 (2认同)

Fon*_*ily 11

我遇到了类似的问题,并查看了这里的许多其他帖子。这对我有用。

我创建了一个初始视图,并将我想要垂直居中的元素嵌套在该视图中。我犯了一个错误,在初始视图中包含“flex:1”,这搞砸了垂直对齐。

代码:

<View style={{flexDirection: 'row', justifyContent: 'center', alignItems: 'center'}}>
  <Text>Content here to be vertically aligned</Text>
  <Switch>...</Switch>
</View>
Run Code Online (Sandbox Code Playgroud)


小智 9

当您使用图像或图标时,尤其会出现此问题。我有同样的问题我的解决方案:

 <View style={{ flex: 1, flexDirection: 'row', justifyContent: 'center', alignItems: 'center' }}>
   <Icon type="MaterialCommunityIcons" name="barcode" />
   <Text style={{ textAlign: 'center' }}>{urun_data.barkod}</Text>
</View>
Run Code Online (Sandbox Code Playgroud)


Jan*_*ara 5

RN 版本 >= 0.55.2 现在支持“textAlignVertical”。

<TextInput
    style={styles.text}
    onChangeText={text => console.log('text >>>>>>>', text)}
    value='sample text'
 />

text: {
  textAlign: 'center',
  textAlignVertical: 'center',
}
Run Code Online (Sandbox Code Playgroud)

处理这两种情况flex: 1height组件集<Text>