- React:Cross-Platform Application Development with React Native
- Emilio Rodriguez Martinez
- 372字
- 2021-07-02 16:17:21
LocationSearch
This should be a simple textbox displaying the human-readable name of the location in which the map is centered. Let's take a look at the code:
/*** src/components/LocationSearch.js ** */ import React from 'react'; import { View, Text, TextInput, ActivityIndicator, StyleSheet, } from 'react-native'; export default class LocationSearch extends React.Component { render() { return ( <View style={styles.container}> <Text style={styles.title}>PICKUP LOCATION</Text> {this.props.value && ( <TextInput style={styles.location} value={this.props.value} /> )} {!this.props.value && <ActivityIndicator style={styles.spinner} />} </View> ); } } const styles = StyleSheet.create({ container: { backgroundColor: 'white', margin: 20, marginTop: 40, height: 60, padding: 10, borderColor: '#ccc', borderWidth: 1, }, title: { alignSelf: 'center', fontSize: 12, color: 'green', fontWeight: 'bold', }, location: { height: 40, textAlign: 'center', fontSize: 13, }, spinner: { margin: 10, }, });
It receives only one property: value
(the name of the location to be displayed). If it's not set, it will display a spinner to show activity.
Because there are many different styles to be applied in this component, it's beneficial to use the StyleSheet
API to organize the styles in a key/value object and refer it from our render
method. This separation between logic and style helps in readability of the code and also enables code reuse as the styles can be cascaded down to child components.
Aligning Elements
React Native uses Flexbox for setting up the layout of the elements in an app. This is mostly straightforward, but sometimes it can be confusing when it comes to aligning elements as there are four properties that can be used for this purpose:
justifyContent:
It defines the alignment of the child elements through the main axisalignItems
: It defines the alignment of the child elements through the cross-axisalignContent
: It aligns a flex container's lines within when there is extra space in the cross-axisalignSelf
: It allows the default alignment (or the one specified byalignItems
) to be overridden for individual flex items
The first three properties should be assigned to the container element, while the fourth one will be applied to a child element in case we want to override the default alignment.
In our case, we only want one element (the title) to be center aligned so we can use alignSelf: 'center'
. Later in this lesson, we will see other uses for the different align
properties.
- 黑客攻防實(shí)戰(zhàn)技術(shù)完全手冊(cè):掃描、嗅探、入侵與防御
- EJB 3.1從入門到精通
- 廣電5G從入門到精通
- 6G潛在關(guān)鍵技術(shù)(下冊(cè))
- 物聯(lián)網(wǎng)(IoT)基礎(chǔ):網(wǎng)絡(luò)技術(shù)+協(xié)議+用例
- 電子政務(wù)效益的經(jīng)濟(jì)分析與評(píng)價(jià)
- 網(wǎng)絡(luò)創(chuàng)新指數(shù)研究
- Yii Application Development Cookbook(Second Edition)
- 物聯(lián)網(wǎng)長(zhǎng)距離無線通信技術(shù)應(yīng)用與開發(fā)
- CCNP TSHOOT(642-832)認(rèn)證考試指南
- Windows Server 2012 Hyper-V虛擬化管理實(shí)踐
- 網(wǎng)管工具使用與技巧大全
- 異構(gòu)蜂窩網(wǎng)絡(luò)關(guān)鍵理論與技術(shù)
- 中國(guó)互聯(lián)網(wǎng)發(fā)展報(bào)告2021
- Web用戶查詢?nèi)罩就诰蚺c應(yīng)用