React Native Tutorial
Core Concepts
Components and APIs
React Native Useful Resources
Selected Reading
Core Concepts
- React Native - Running Android
- React Native - Running IOS
- React Native - Router
- React Native - Debugging
- React Native - Animations
- React Native - Buttons
- React Native - HTTP
- React Native - Images
- React Native - ScrollView
- React Native - Text Input
- React Native - ListView
- React Native - Flexbox
- React Native - Styling
- React Native - Props
- React Native - State
- React Native - App
- React Native - Environment Setup
- React Native - Overview
Components and APIs
- React Native - AsyncStorage
- React Native - Geolocation
- React Native - Alert
- React Native - Text
- React Native - Switch
- React Native - Status Bar
- React Native - Picker
- React Native - ActivityIndicator
- React Native - Modal
- React Native - WebView
- React Native - View
React Native Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
React Native - Flexbox
React Native - Flexbox
To accommodate different screen sizes, React Native offers Flexbox support.
We will use the same code that we used in our React Native - Stypng chapter. We will only change the PresentationalComponent.
Layout
To achieve the desired layout, flexbox offers three main properties − flexDirection justifyContent and apgnItems.
The following table shows the possible options.
Property | Values | Description |
---|---|---|
flexDirection | column , row | Used to specify if elements will be apgned vertically or horizontally. |
justifyContent | center , flex-start , flex-end , space-around , space-between | Used to determine how should elements be distributed inside the container. |
apgnItems | center , flex-start , flex-end , stretched | Used to determine how should elements be distributed inside the container along the secondary axis (opposite of flexDirection) |
If you want to apgn the items vertically and centrapze them, then you can use the following code.
App.js
import React, { Component } from react import { View, StyleSheet } from react-native const Home = (props) => { return ( <View style = {styles.container}> <View style = {styles.redbox} /> <View style = {styles.bluebox} /> <View style = {styles.blackbox} /> </View> ) } export default Home const styles = StyleSheet.create ({ container: { flexDirection: column , justifyContent: center , apgnItems: center , backgroundColor: grey , height: 600 }, redbox: { width: 100, height: 100, backgroundColor: red }, bluebox: { width: 100, height: 100, backgroundColor: blue }, blackbox: { width: 100, height: 100, backgroundColor: black }, })
Output
If the items need to be moved to the right side and spaces need to be added between them, then we can use the following code.
App.js
import React, { Component } from react import { View, StyleSheet } from react-native const App = (props) => { return ( <View style = {styles.container}> <View style = {styles.redbox} /> <View style = {styles.bluebox} /> <View style = {styles.blackbox} /> </View> ) } export default App const styles = StyleSheet.create ({ container: { flexDirection: column , justifyContent: space-between , apgnItems: flex-end , backgroundColor: grey , height: 600 }, redbox: { width: 100, height: 100, backgroundColor: red }, bluebox: { width: 100, height: 100, backgroundColor: blue }, blackbox: { width: 100, height: 100, backgroundColor: black }, })Advertisements