React Native 結合了 Web 與原生應用的優勢,讓你能用 JavaScript 開發 iOS 與 Android 原生 App。它以 React 來抽象操作系統的原生 UI 元件,取代瀏覽器的 DOM 元素來進行渲染。換句話說,你只要學會一套 JavaScript 與 React 的開發方式,就能在各平台上高效地打造應用體驗——這也是 Facebook 在多個產品中採用並持續投入的原因。
接觸 React Native 兩個多月,從最開始的頁面搭建、點擊事件,到呼叫後端接口大致都摸過一輪,這次就來做一個最經典的練習:登入畫面。

1、React Native 中的 <TextInput> 預設會帶一條底線,若想移除,只要給它指定 underlineColorAndroid='transparent' 即可;
2、密碼欄位需要加上屬性 secureTextEntry={true};
3、要顯示圖片時,必須為 <Image> 明確指定寬高。與 Android 不同,它無法自動縮放圖片,也沒有類似 wrap_content 的機制。
/** * Sample React Native App * https://github.com/facebook/react-native * @flow */ import React, {Component} from 'react'; import { AppRegistry, StyleSheet, Text, Image, View, TextInput } from 'react-native'; class ReactDemo extends Component { render() { return ( <View style={styles.container}> <View style={styles.header}> <Text style={styles.headtitle}>添加賬號</Text> </View> <View style={styles.marginTopview}/> <View style={styles.inputview}> <TextInput underlineColorAndroid='transparent' style={styles.textinput} placeholder='QQ號/手機號/郵箱'/> <View style={styles.dividerview}> <Text style={styles.divider}></Text> </View> <TextInput underlineColorAndroid='transparent' style={styles.textinput} placeholder='密碼' secureTextEntry={true}/> </View> <View style={styles.bottomview}> <View style={styles.buttonview}> <Text style={styles.logintext}>登 錄</Text> </View> <View style={styles.bottombtnsview}> <View style={styles.bottomleftbtnview}> <Text style={styles.bottombtn}>無法登錄?</Text> </View> <View style={styles.bottomrightbtnview}> <Text style={styles.bottombtn}>新用戶</Text> </View> </View> </View> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#FFFFFF' }, header: { height: 50, backgroundColor: '#12B7F5', justifyContent: 'center', }, headtitle: { alignSelf: 'center', fontSize: 20, color: '#ffffff', }, avatarview: { height: 150, backgroundColor: '#ECEDF1', justifyContent: 'center', }, avatarimage: { width: 100, height: 100, alignSelf: 'center' }, marginTopview: { height: 15, backgroundColor: '#F7F7F9' }, inputview: { height: 100, }, textinput: { flex: 1, fontSize: 16, }, dividerview: { flexDirection: 'row', }, divider: { flex: 1, height: 1, backgroundColor: '#ECEDF1' }, bottomview: { backgroundColor: '#ECEDF1', flex: 1, }, buttonview: { backgroundColor: '#1DBAF1', margin: 10, borderRadius: 6, justifyContent: 'center', alignItems: 'center', }, logintext: { fontSize: 17, color: '#FFFFFF', marginTop: 10, marginBottom: 10, }, emptyview: { flex: 1, }, bottombtnsview: { flexDirection: 'row', }, bottomleftbtnview: { flex: 1, height: 50, paddingLeft: 10, alignItems: 'flex-start', justifyContent: 'center', }, bottomrightbtnview: { flex: 1, height: 50, paddingRight: 10, alignItems: 'flex-end', justifyContent: 'center', }, bottombtn: { fontSize: 15, color: '#1DBAF1', } }); AppRegistry.registerComponent('ReactDemo', () => ReactDemo);
整個畫面以 View 分層堆疊,再用 StyleSheet 統一管理樣式,結構清楚、易於擴充。以上就是用 React Native 實作登入畫面的完整示範,希望對你有所幫助,若有疑問歡迎留言交流。