前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >React Native组件布局应用示例小结

React Native组件布局应用示例小结

作者头像
星宇大前端
发布2019-01-15 15:33:00
5670
发布2019-01-15 15:33:00
举报
文章被收录于专栏:大宇笔记大宇笔记

序:

     学完了Flex布局和Text Image组件,感觉可以总结一下了。因为本人对英雄联盟比较感兴趣,所以准备把前面学习的做一个应用小例子。

     都是前面文章提到内容,主要练习下熟练度,就不写备注了。

先看下效果:

代码如下:

代码语言:javascript
复制
import React, { Component } from 'react';
import {
   AppRegistry,
  StyleSheet,
  Text,
  View,
  Image,
} from 'react-native';

var heroData = require('./ImageData.json');

class RNHybrid extends Component {

  render() {  
      return(  
        <View style={styles.container}>  
            {this.renderLoadForView()}  
        </View>  
      );  
   }

   renderLoadForView(){  
    var forView = [];
    var heroArray = heroData.data
    for (var i = 0; i < heroArray.length; i++) {  
    	var hero =  heroArray[i];
        forView.push(
        <View key={i} style={styles.containViewStyle}>
        <Image source={{uri: hero.img}}  style={styles.imageStyle}/>  
        <Text key={i}>
           {hero.title}
        </Text>
        </View>
      );  
    }  
    return forView;  
  }  
}


const styles = StyleSheet.create({
   container:{
   		marginTop:100,
   		flexDirection:'row',
      flexWrap:'wrap',
      justifyContent:'space-around',
   },
   containViewStyle:{
      alignItems:'center',
      justifyContent:'center',
      width:120,
      height:120,
      borderColor:'black',
      borderWidth:1,
      marginBottom:10,
   },
   imageStyle:{
      width:60,
      height:60,
   },
});

AppRegistry.registerComponent('RNHybrid', () => RNHybrid);

前面博客没有提到的知识点说明:

1、var heroData = require('./ImageData.json');

看下Json文件:

代码语言:javascript
复制
{
  "data": [
    {
      "img" : "hero1",
      "title" : "木木"
    },
    {
      "img" : "hero2",
      "title" : "剑姬"
    },
    {
      "img" : "hero3",
      "title" : "船长"
    },
    {
      "img" : "hero4",
      "title" : "日女"
    },
    {
      "img" : "hero5",
      "title" : "锤石"
    },
    {
      "img" : "hero6",
      "title" : "老鼠"
    }
  ]
}

解答:这句话的意思是拿到json这个对象,就相当于OC中拿到一个解析好的Json文件。剩下的大家都知道了。

2、图片位置:

解答:图片我都放在Asset里面了,当然你也可以网络加载,或者放在RN项目中。

3、循环添加视图,JSX语法有说可以看下。JSX语法

资源文件:https://pan.baidu.com/s/1i5l95xb

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年08月16日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档