import {StyleSheet, Text, View} from 'react-native'; import React from 'react'; const tangPoetry = ( <> <Text>清明时节雨纷纷,</Text> <Text>路上行人欲断魂。</Text> <Text>借问酒家何处有,</Text> <Text>牧童遥指杏花村</Text> </> ); export default function index() { return ( <View> {/* view container */} <View>{tangPoetry}</View> {/* text container */} <Text>{tangPoetry}</Text> {/* 嵌套文本 */} <Text style={styles.baseText}> 总金额: <Text style={styles.innerText}>¥3999</Text> </Text> {/* view 中直接放一段文本 */} {/* <View>view中直接放文本(错误)</View> */} {/* Render Error: Text strings must be rendered width a <Text> component */} {/* 查看view样式继承 */} <View style={{fontSize: 30, color: 'geekblue'}}> <Text>查看 View 组件的样式能否继承</Text> </View> {/* 继承只发生在 Text 组件上 */} <Text style={{fontSize: 30, color: 'lightblue'}}> <Text>继承只发生在 Text 组件上</Text> <Text>观察字体的大小和颜色变化</Text> </Text> </View> ); } const styles = StyleSheet.create({ baseText: { fontWeight: '600', }, innerText: { color: 'tomato', }, });
View 创建 UI 时最基础的组件,支持 flexbox 布局、样式、触摸响应和一些无障碍的内容
import {StyleSheet, Text, View} from 'react-native'; import React from 'react'; export default function index() { return ( <View style={styles.container}> <View style={styles.top} /> <View style={styles.middle} /> <View style={styles.bottom} /> </View> ); } const styles = StyleSheet.create({ container: { height: '100%', justifyContent: 'space-between', backgroundColor: '#fff', padding: 20, }, top: { flex: 0.3, backgroundColor: 'grey', borderWidth: 5, borderTopLeftRadius: 20, borderTopRightRadius: 20, }, middle: { flex: 0.3, backgroundColor: 'beige', borderWidth: 5, }, bottom: { flex: 0.3, backgroundColor: 'pink', borderWidth: 5, borderBottomLeftRadius: 20, borderBottomRightRadius: 20, }, });
Button
import {View, Button, Alert} from 'react-native'; import React from 'react'; export default function index() { return ( <View style={{height: 200, padding: 20, justifyContent: 'space-between'}}> <Button title="确认" onPress={() => Alert.alert('被点击了')} /> <Button title="取消" color="tomato" onPress={() => alert('点击了取消')} /> <Button title="禁用" disabled /> </View> ); }
Alert
import {StyleSheet, Text, View, Button, Alert} from 'react-native'; import React from 'react'; export default function index() { const showAlertOne = () => { Alert.alert('温馨提示1', '查看提示框消息', [ {text: '确认', onPress: () => console.log('点击确认按钮的回调函数')}, ]); }; const showAlertTwo = () => { Alert.alert('温馨提示2', '查看提示框消息', [ {text: '确认', onPress: () => console.log('点击确认按钮的回调函数')}, {text: '取消', onPress: () => console.log('点击取消按钮的回调函数')}, ]); }; const showAlertThree = () => { Alert.alert('温馨提示3', '查看提示框消息', [ {text: '再看看', onPress: () => console.log('点击再看看按钮的回调函数')}, {text: '确认', onPress: () => console.log('点击确认按钮的回调函数')}, {text: '取消', onPress: () => console.log('点击取消按钮的回调函数')}, ]); }; return ( <View style={{height: '100%', padding: 20, justifyContent: 'space-around'}}> <Button title="一个按钮" color="chocolate" onPress={showAlertOne} /> <Button title="两个按钮" color="cornflowerblue" onPress={showAlertTwo} /> <Button title="三个按钮" color="crimson" onPress={showAlertThree} /> </View> ); }
StatusBar 控制应用状态栏的组件 StatusBar 可以在任意视图中加载,可以放置多个,后加载的会覆盖先加载的
import {View, Text, StatusBar, Switch} from 'react-native'; import React, {useState} from 'react'; export default function index() { const [hideStatusBar, setHideStatusBar] = useState(false); const toggleStatusBar = () => { setHideStatusBar(!hideStatusBar); }; return ( <View style={{alignItems: 'flex-start'}}> <StatusBar hidden={hideStatusBar} animated backgroundColor="#09b807" barStyle="dark-content" /> <Switch value={hideStatusBar} thumbColor={hideStatusBar ? '#f5dd4b' : '#f4f'} trackColor={{false: '#767577', true: '#81b0ff'}} onValueChange={toggleStatusBar} /> </View> ); }
ActivityIndicator 显示一个圆形的 loading 提示符号
import {View, ActivityIndicator, StyleSheet} from 'react-native'; import React from 'react'; export default function index() { return ( <View style={[styles.container]}> <ActivityIndicator /> <ActivityIndicator size="large" /> <ActivityIndicator size="small" color="#0000ff" /> <ActivityIndicator size="large" color="#00ff00" /> {/* 数字指定大小,只在 Android 应用下有效 */} <ActivityIndicator color="tomato" size={70} /> <ActivityIndicator color="pink" size={100} /> </View> ); } const styles = StyleSheet.create({ container: { height: '100%', padding: 20, flexDirection: 'row', justifyContent: 'center', alignItems: 'flex-start', }, });