ReactNative爬坑-小记<Image/>控件通过json数据加载App里drawable中图片资源

来源:互联网 发布:linux make 退出 编辑:程序博客网 时间:2024/06/11 03:24

环境: Android 5.0、RN0.46.4

1、将图片文件导入进Android资源文件drawable根目录下(必须是根目录);

2、建立json文件,将图片资源名(无后缀)写入:

{
    "data":[
        {
            "text": "大象",
            "icon": "elephant"
        },
        {
            "text": "公鸡",
            "icon": "cock"
        }
    ]
}

3、json文件读取:

var data = require("./data.json");

。。。。

showView(){
    var cmp = [];
    for (var i = 0; i < data.data.length; i++) {
      let obj = data.data[i];
      cmp.push(<View key={i} style={styles.cmpStyle}>
        <Image source={{uri: obj.icon}} style={styles.imgStyle}/>
        <Text>{obj.icon}</Text>
        <Text>{obj.text}</Text>
      </View>
      );
  }
  return cmp; //返回控件数组

}

4、showVeiw()调用render显示:

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

结果:


阅读全文
0 0
原创粉丝点击