前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >React-Native入门指南(三)

React-Native入门指南(三)

作者头像
疯狂的技术宅
发布2019-03-28 16:51:10
9870
发布2019-03-28 16:51:10
举报
文章被收录于专栏:京程一灯京程一灯

今天为大家更新React-Native入门指南(三),纯干货,请偷偷观看哦!

五、React-Native布局实战(二)

在不断深入的过程中,发现React-Native布局和样式的坑还有很多,他没有像浏览器那样灵活和有规律可循,其中的规律需要我自己踩坑的时候发现。比如:不存在zIndex,后面的元素覆盖前面的元素;内层元素覆盖外层元素等等,borderRadius的设置,需要考虑到内层元素的位置等等。

1、实战的内容

这里选用携程的App首页作为栗子,随不是严格的9宫格(比9宫格稍微难点...),但是可以很好的让我们练习flexbox.最后需要完成的结果如下:

2、分解内容

整个页面我们可以分为几个部分,大致如下:

  • 头部
  • 图片轮播
  • 9宫格
  • 底部活动

3、头部导航栏

因为,组件还没有讲,这里只是做一个简单的介绍。在React-Native中实现头部导航栏很简单,只要使用NavigatorIOS组件即可。现在开工。 (1)我们在index.ios.js中添加如下代码;同时创建文件夹pagaes和pages下创建Index.js

var React = require('react-native'); var Index = require('./pages/Index'); var { NavigatorIOS, AppRegistry, StyleSheet, } = React; var NV = React.createClass({ render: function(){ return( <NavigatorIOS style={styles.container} initialRoute={{ title: '首页', component: Index, }} /> ); } });

var styles = StyleSheet.create({ container: { flex: 1, } }); AppRegistry.registerComponent('HelloWorld', () => NV); 分析代码: 1)require:引入外部模块,就像,引入我们自己创建的/pages/Index.js一样。 2)引入定义NavigatorIOS、AppRegistry、StyleSheet组件和类。 3)在render中调用NavigatorIOS组件,initialRoute是初始化路由,title是当前页面的头部标题;component是当前路由下显示的组件; 4)注意:这里NavigatorIOS的style需要设置大小,比如这里设置是flex:1,否则就不能显示内容主体; 5)最后我们需要注册当前应用:AppRegistry.registerComponent('HelloWorld', () => NV); (2)创建Index.js文件,文件的内容如下, module.exports就暴露了Index模块。

效果如下图:

4、图片轮播

这里图片轮播使用的是第三方组件react-native-swiper,当然React-Native是支持transform可以直接实现一套。

(1)我们启动npm命令行,在项目的根目录使用如下命令安装模块。

$ npm install react-native-swiper --save $ npm i react-timer-mixin --save

(2)需要关闭React packager命令行和模拟器,在xcode中重启项目

安装完成后,我们需要完成轮播功能。因为可以到github看看swiper暴露的接口和参数。github地址是:

https://github.com/leecade/react-native-swiper 1)引入swiper,前面也提到了require. var Swiper = require('react-native-swiper'); 2)使用swiper,将轮播图封装成单独的组件 var sliderImgs = [ 'http://images3.c-ctrip.com/SBU/apph5/201505/16/app_home_ad16_640_128.png', 'http://images3.c-ctrip.com/rk/apph5/C1/201505/app_home_ad49_640_128.png', 'http://images3.c-ctrip.com/rk/apph5/D1/201506/app_home_ad05_640_128.jpg' ]; var Slider = React.createClass({ render: function(){ return ( <Swiper style={styles.wrapper} showsButtons={false} autoplay={true} height={150} showsPagination={false}> <Image style={[styles.slide,]} source={{uri: sliderImgs[0]}}></Image> <Image style={[styles.slide,]} source={{uri: sliderImgs[1]}}></Image> <Image style={[styles.slide,]} source={{uri: sliderImgs[2]}}></Image> </Swiper> ); } }); 3)这样我们可以直接在render的时候直接用:<Slider/>

5、完成第一个9宫格布局,后面复制拷贝

其实4个九宫格都是一样,这个其实可以封装成组件,这里采用拷贝的形式,开发一个,其他3个就ok的,不会偷懒的工程师,不是好工程师[偷笑]。分析下布局:

(1)其实首先是3个列在一行的布局,那么外层组件是需要flexDirection: 'row',各占据宽度的1/3,即各自flex:1; (2)每个列内又分两行, 需要每个行都是flex:1,各占据高度的一半; (3)第一列是文字图片组合,其余都是文字组合; (4)所有行内元素都是水平、垂直居中; (5)这里使用了个TouchableHighlight组件,是为了出发onPress事件,类似于click或者touch事件。 <View style={[styles.sbu_red, styles.sbu_view]}> <TouchableHighlight underlayColor={'#FA6778'} style={{flex:1}}> <View style={[styles.sbu_flex, styles.sbu_borderRight]}> <View style={[styles.sub_con_flex, styles.sub_text]}> <Text style={[styles.font16]}>酒店</Text> </View> <View style={[styles.sub_con_flex]}> <Image style={[styles.sbu_icon_img]} source={{uri:BUIcon[0]}}></Image> </View> </View> </TouchableHighlight> <View style={[styles.sbu_flex, styles.sbu_borderRight]}> <View style={[styles.sub_con_flex, styles.sub_text, styles.sbu_borderBottom]}> <Text style={[styles.font16]}>海外</Text> </View> <View style={[styles.sub_con_flex, styles.sub_text]}> <Text style={[styles.font16]}>周边</Text> </View> </View>

<View style={[styles.sbu_flex]}> <View style={[styles.sub_con_flex, styles.sub_text, styles.sbu_borderBottom]}> <Text style={[styles.font16]}>团购.特惠</Text> </View> <View style={[styles.sub_con_flex, styles.sub_text]}> <Text style={[styles.font16]}>客栈.公寓</Text> </View> </View> </View>

6、样式类

说完了布局的原理,这里需要贴上样式仅供参考:

var styles = StyleSheet.create({ //container container:{ backgroundColor:'#F2F2F2', flex:1, }, //slider wrapper: { height:80, }, slide: { height:80, resizeMode: Image.resizeMode.contain, }, //sbu sbu_view:{ height:84, marginLeft: 5, marginRight:5, borderWidth:1, borderRadius:5, marginBottom:10, flexDirection:'row', },

sbu_red:{ backgroundColor: '#FA6778', borderColor:'#FA6778', marginTop:-70, }, sbu_blue:{ backgroundColor: '#3D98FF', borderColor:'#3D98FF', }, sbu_green:{ backgroundColor: '#5EBE00', borderColor:'#5EBE00', }, sbu_yellow:{ backgroundColor: '#FC9720', borderColor:'#FC9720', }, sbu_flex:{ flex:1, },

sbu_borderRight:{ borderColor:'#fff', borderRightWidth: 0.5, }, sbu_icon_img:{ height:40, width:40, resizeMode:Image.resizeMode.contain, }, sub_con_flex:{ flex:1, justifyContent: 'center', alignItems: 'center', }, sub_text:{ justifyContent:'center', }, font16:{ fontSize:17, color:'#FFF', fontWeight:'900', }, sbu_borderBottom:{ borderBottomWidth:0.5, borderBottomColor:'#fff', },

img_view:{ height:62, marginLeft:5, marginRight:5, flexDirection: 'row', marginBottom:20, backgroundColor:'#fff', }, img_flex:{ flex:1, borderWidth:1, borderColor:'#ccc', }, img_wh: { height:59, borderRightWidth:0, resizeMode:Image.resizeMode.contain, } });

宽度或者高度而自适应大小的,因此我们需要让图片根据宽度或者高度来自适应,那么可以使用resizeMode:Image.resizeMode.contain。facebook提示错误信息的样式表中也没有提及,文档中也没有提及。所以后续还有不少的坑需要大家去一起探索。

7、Index.js整个代码,仅供参考

实例代码中会涉及ScrollView组件,主要是为了适应小屏的机器,可以滚动视图。

具体代码可参考https://github.com/vczero/react-native-lesson

本文转载自github大咖个人博客,原作者授权发布

原作者:vczero

转自:https://github.com/vczero/react-native-lesson

那么多代码,是不是看着很枯燥呢?

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-11-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 京程一灯 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 五、React-Native布局实战(二)
    • 1、实战的内容
      • 2、分解内容
        • 3、头部导航栏
          • 4、图片轮播
            • 5、完成第一个9宫格布局,后面复制拷贝
              • 6、样式类
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档