Creación de estilos, layouts flexibles y grids en React Native.
StyleSheet API y Flexbox
Uso de StyleSheet, estilos por plataforma, layouts flexibles y grids.
typescript1import { View, Text, StyleSheet, Platform, TouchableOpacity } from 'react-native' 2 3// StyleSheet básico 4function StyledComponent() { 5 return ( 6 <View style={styles.container}> 7 <Text style={styles.text}>Hola Mundo</Text> 8 <TouchableOpacity style={[styles.button, styles.primaryButton]}> 9 <Text style={styles.buttonText}>Presionar</Text> 10 </TouchableOpacity> 11 </View> 12 ) 13} 14 15// Flexbox Examples 16function FlexboxExamples() { 17 return ( 18 <View style={styles.flexContainer}> 19 {/* Row Layout */} 20 <View style={styles.row}> 21 <View style={[styles.box, { backgroundColor: '#FF6B6B' }]} /> 22 <View style={[styles.box, { backgroundColor: '#4ECDC4' }]} /> 23 <View style={[styles.box, { backgroundColor: '#45B7D1' }]} /> 24 </View> 25 26 {/* Column Layout */} 27 <View style={styles.column}> 28 <View style={[styles.box, { backgroundColor: '#FFA07A' }]} /> 29 <View style={[styles.box, { backgroundColor: '#98D8C8' }]} /> 30 </View> 31 32 {/* Centered Layout */} 33 <View style={styles.centered}> 34 <Text style={styles.centeredText}>Centrado</Text> 35 </View> 36 </View> 37 ) 38} 39 40// Grid Layout 41function GridLayout() { 42 const items = Array.from({ length: 6 }, (_, i) => i + 1) 43 44 return ( 45 <View style={styles.grid}> 46 {items.map((item) => ( 47 <View key={item} style={styles.gridItem}> 48 <Text style={styles.gridItemText}>{item}</Text> 49 </View> 50 ))} 51 </View> 52 ) 53} 54 55const styles = StyleSheet.create({ 56 container: { 57 flex: 1, 58 padding: 16, 59 backgroundColor: '#f5f5f5' 60 }, 61 text: { 62 fontSize: 16, 63 ...Platform.select({ 64 ios: { 65 fontFamily: 'System', 66 fontWeight: '500' 67 }, 68 android: { 69 fontFamily: 'Roboto', 70 fontWeight: 'normal' 71 } 72 }) 73 }, 74 button: { 75 padding: 12, 76 borderRadius: 8, 77 alignItems: 'center' 78 }, 79 primaryButton: { 80 backgroundColor: '#007AFF' 81 }, 82 buttonText: { 83 color: 'white', 84 fontSize: 16, 85 fontWeight: '600' 86 }, 87 flexContainer: { 88 flex: 1, 89 gap: 16 90 }, 91 row: { 92 flexDirection: 'row', 93 justifyContent: 'space-between', 94 gap: 8 95 }, 96 column: { 97 flexDirection: 'column', 98 gap: 8 99 }, 100 box: { 101 width: 80, 102 height: 80, 103 borderRadius: 8 104 }, 105 centered: { 106 height: 100, 107 justifyContent: 'center', 108 alignItems: 'center', 109 backgroundColor: '#E8E8E8', 110 borderRadius: 8 111 }, 112 centeredText: { 113 fontSize: 18, 114 fontWeight: 'bold' 115 }, 116 grid: { 117 flexDirection: 'row', 118 flexWrap: 'wrap', 119 gap: 8, 120 padding: 8 121 }, 122 gridItem: { 123 width: '31%', 124 aspectRatio: 1, 125 backgroundColor: '#007AFF', 126 justifyContent: 'center', 127 alignItems: 'center', 128 borderRadius: 8 129 }, 130 gridItemText: { 131 color: 'white', 132 fontSize: 24, 133 fontWeight: 'bold' 134 } 135})
Recursos
1 recurso disponible
Reto de Lectura
Reto de LecturaPrincipiante30 min🔴 Sin IA
Reto de Lectura — Los estilos que no flexionan
Este layout se ve perfecto en el teléfono del que lo hizo… y roto en cualquier otro. Encuentra los 4 anti-patrones de flexibilidad.
Esta pantalla de tarjeta de contacto "se ve perfecta" en un iPhone 14. Encuentra los 4 anti-patrones que la rompen en un teléfono chico, uno grande, o al girar la pantalla, y corrígelos.
jsx1const styles = StyleSheet.create({ 2 pantalla: { 3 width: 390, 4 height: 844, 5 backgroundColor: "#fff", 6 }, 7 fila: { 8 flexDirection: "row", 9 width: 390, 10 }, 11 avatar: { 12 width: 80, 13 height: 80, 14 borderRadius: 40, 15 }, 16 info: { 17 width: 280, 18 marginLeft: 10, 19 }, 20 nombre: { 21 fontSize: 20, 22 width: 280, 23 }, 24 boton: { 25 position: "absolute", 26 top: 780, 27 left: 20, 28 width: 350, 29 height: 48, 30 }, 31});
Preguntas:
- ¿De dónde salieron 390 y 844? ¿Qué pasa en un teléfono de 360 de ancho?
- ¿Cómo debería ocupar
info"el espacio restante" sin conocer el ancho del teléfono? - ¿Qué le pasa al botón anclado con
top: 780en una pantalla más corta? ¿Cuál es la forma flexible de "botón al fondo"? - ¿Cuáles de estos números fijos SÍ son legítimos?
La solución no se publica. Se revisa en voz alta en el laboratorio, después de que trabajaste el reto. Trae tu diagnóstico escrito: eso es lo que se entrega.
Clase 8 de 32 en la ruta Desarrollo movil