簡體   English   中英

無法在 React Native 中對齊文本

[英]Unable to align text in React Native

我是 React Native 的新手。 我用它創建了一個登錄頁面。 我想知道如何在左側對齊Login文本,而不是在中心顯示。

在此處輸入圖像描述

我的代碼:

登錄.js

import React from 'react';
import { View, Text, StyleSheet, TextInput, TouchableOpacity } from 'react-native';

const Login = () => {

  return (

     <View style={styles.container}>
        <Text style={styles.head}>
             Login
        </Text>
        <TextInput placeholder="Username" style={styles.input}/>
        <TextInput placeholder="Password" style={styles.input}/>
        <TouchableOpacity style={styles.button}>
            <Text style={styles.textStyle}>LOGIN</Text>
        </TouchableOpacity>
    </View>
  )
}

const styles = StyleSheet.create({
container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor:'white'
},
head:{
   alignItems:'flex-start',
   fontWeight:'bold',
   fontSize:30,
   marginBottom:15
},
input:{
    height:50,
    borderRadius:5,
    backgroundColor:'#ededed',
    alignSelf:'stretch',
    marginLeft:15,
    marginRight:15,
    marginBottom:8,
    paddingLeft:15
},
button:{
    backgroundColor:'#e57373',
    alignSelf:'stretch',
    borderRadius:5,
    marginLeft:15,
    marginRight:15,
    alignItems:'center',
    padding: 15,
},
textStyle:{
    fontWeight:'bold',
    color:'white'
}
});

export default Login;

讓我知道我在上面的代碼中做錯了什么。

我現在無法測試它,但它應該是這樣的:

alignSelftextAlign屬性添加到您的head樣式中。

head: {
    alignItems:'flex-start',
    alignSelf: 'stretch',
    fontWeight:'bold',
    fontSize:30,
    marginBottom:15,
    textAlign: 'left'
},

alignItems 關注父容器的子元素,而 alignSelf 處理元素本身。

head:{
   alignSelf:'flex-start',
   // ...other styles
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM