[教程] React Native基础实战(1)—— 制作一个简单的按钮
来源:互联网 发布:淘宝运营规则是什么 编辑:程序博客网 时间:2024/06/09 18:07
开始前先说点什么
开始使用React Native后你会很快发现,在其组件库中找不到一个好用的按钮。虽然,在最近的更新中已经出现了一个Button
组件,但是这个Button
的外观在Adroid和iOS系统下的外观截然不同(见下图),所以这就使得做一个按钮变得非常重要。虽然有很多第三方的包都有按钮,但是即使是为了理解React Native的工作原理,自己做一个按钮也是非常有必要的。
本教程使用Expo Snack制作,这是一款非常好用的工具,可以让你在线调试React Native,并且还预装了一些常用的库。项目完成效果请见https://snack.expo.io/Sy7Utsv-W, 如果登陆有问题则可能需要翻墙。
从结果出发思考需求
使用React的一大优势就是他基于组件的设计,可以开发一次然后随处使用。所以我们首先要做的是从使用者的角度思考,这个组件应该具有哪些KPI,然后再考虑如何实现。这可以称为Wish-Driven Design(基于想象的设计)。
假设我们要做后图中的按钮,那么我希望只需要写一样代码,这里所有的颜色和样式都是默认的:
<Button tag="Press Me" onPress={this.onPress}/>
但是同时,我还希望能够方便的对于按钮的颜色和字体等进行定制,所以Button组件还应该能够这样使用:
<Button tag="Press Me" onPress={() => Alert.alert("Pressed...")} buttonStyle={[styles.button]} textStyle={[styles.textStyle]} />const styles = StyleSheet.create({ button: { backgroundColor: 'blue' }, textStyle: { fontSize: 25, }})
代码实现
一个Button由三个部分构成:
- TouchableOpacity
组件在每次点击时都会改变其下所有子组件的透明度,这样做是为了给用户反馈,告诉他们你已经点击了这个按钮。该组件广泛用于各种会被点击的组件。
- View
负责按钮的外观,这里主要是长、宽和背景颜色等。
- Text
负责文字的外观
class Button extends Component { render() { return ( <TouchableOpacity onPress={this.props.onPress}> <View style={[buttonStyles.button, this.props.buttonStyle]}> <Text style={[buttonStyles.text, this.props.textStyle]}>{this.props.tag}</Text> </View> </TouchableOpacity> ); }}
下面就几个关键点做一下说明:
将组件长度设置为父组件长度
React Native的组件位置和长宽等一般是通过flexbox模型进行控制的,这个模型和css中的基本一致。如果对css不熟悉,请参考相关文档并玩玩这个游戏:https://flexboxfroggy.com/。
这里View
在没有任何设置的情况下只会取Text
组件的长宽,紧紧围绕在Text的周围。所以我们使用padding
让按钮有了高度,而对于宽度,我们希望能够充满父组件的宽度。这里width
设置为100%是不被React Native支持的,我们于是选择设置alignSelf
属性为'stretch'
。
button: { alignSelf: 'stretch', },
使props中的样式覆盖组件的默认样式
在需求中我们提到,希望能够在需要的时候设置按钮的样式。所以我们在设置样式的时候使用了这样的语法:
<View style={[buttonStyles.button, this.props.buttonStyle]}>
在React Native运行的时候,他会使用后面的样式覆盖前面的,其机制和css类似。
完整代码展示
import React, { Component } from 'react';import { TouchableOpacity, View, Text, StyleSheet, Alert } from 'react-native';import { Constants } from 'expo';class Button extends Component { render() { return ( <TouchableOpacity onPress={this.props.onPress}> <View style={[buttonStyles.button, this.props.buttonStyle]}> <Text style={[buttonStyles.text, this.props.textStyle]}>{this.props.tag}</Text> </View> </TouchableOpacity> ); }}const buttonStyles = StyleSheet.create({ button: { backgroundColor: '#841584', padding: 13, alignSelf: 'stretch', alignItems: 'center' }, text: { fontSize: 18, color: '#fff', fontWeight: 'bold', }});export default class App extends Component { render() { return ( <View style={[styles.container]}> <Button tag="Press Me" onPress={() => Alert.alert("Pressed...")} buttonStyle={[styles.button]} textStyle={[styles.textStyle]} /> </View> ); }}const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', paddingTop: Constants.statusBarHeight, backgroundColor: '#ecf0f1', paddingHorizontal: 5, }, button: { backgroundColor: 'blue' }, textStyle: { fontSize: 25, }})
版权声明:本作品采用知识共享署名 3.0 未本地化版本许可协议进行许可,欢迎转载,请注明转载自Andrew Xie的学习笔记(http://blog.csdn.net/andrewxy)。
- [教程] React Native基础实战(1)—— 制作一个简单的按钮
- [教程] ListView的使用和封装——实战React Native中级案例01
- 【React Native实战教程】GitHub Trending API数据的获取
- 一个上架了的React Native项目实战总结
- 一个上架了的React Native项目实战总结
- 一个上架了的React Native项目实战总结
- React Native的按钮详解
- React Native按钮的实现
- 一个简单组件的制作过程——delphi教程
- 【React Native】实战写一个电影列表页(六)
- React Native Android入门实战及深入源码分析系列(1)——Hello world
- ReactNative教程 — Hello React Native
- React Native Android入门实战及深入源码分析系列(2)——React Native源码编译
- React Native 遇到的问题(—)
- react native的单选按钮react-native-radio-master
- React Native入门基础——Props
- react-native的使用教程
- 一个非常简单的react native计时器demo
- HDFS
- Windows RabbitMQ服务端安装与配置详解
- Java面向对象编程
- 什么是分布式缓存框架以及分布式缓存框架的优缺点
- nodejs(koajs)设置中文cookie无效
- [教程] React Native基础实战(1)—— 制作一个简单的按钮
- 【剑指offer-解题系列(24)】二叉树中和为某一值
- eclipse添加代码提示
- 软件架构模式
- 【剑指offer-解题系列(21)】栈的压入、弹出序列
- 安卓错误:cannot be resolved or is not a field
- eclipse启动时内存溢出的解决办法
- 学习笔记TF015:加载图像、图像格式、图像操作、颜色
- 浅谈MVP架构在Android中的应用