基础篇章:React Native之 Image 的讲解
我们先看例子,看看加载本地图片和远程服务器图片的方式,其实差不多。 import React,{ Component } from 'react';
import { AppRegistry,View,Image } from 'react-native';
class DisplayAnImage extends Component {
render() {
return (
<View> <Image source={require('./img/favicon.png')} /> <Image style={{width: 50,height: 50}} source={{uri: 'https://facebook.github.io/react/img/logo_og.png'}} /> </View> ); } }
第一个就是直接在source里写相对路径,第二个就是直接写图片的服务器地址即可。 当然它也支持在android中显 示GIF 和 WebP 图片,方式如下: 属性
Image的style
例子实践看看我模仿的QQ上的一个页面,漂不漂亮?说实话,敲代码这个东西,还得实践,看看这个效果,通过这几篇简单的学习,你能实现吗? 效果图例子代码import React,{ Component } from 'react';
import {
AppRegistry,StyleSheet,Text,Image
} from 'react-native';
class ImageDemo extends Component {
render() {
return (
<View style={styles.container}> <View style={styles.title_view}> <Text style={styles.title_text}> 空间动态 </Text> </View> <View style={styles.three_image_view}> <View style={styles.vertical_view}> <Image source={require('./img/igs.png')} style={{alignSelf:'center',width:45,height:45}} /> <Text style={styles.top_text}> 好友动态 </Text> </View> <View style={styles.vertical_view}> <Image source={require('./img/eqc.png')} style={{alignSelf:'center',height:45}}/> <Text style={styles.top_text}> 附近 </Text> </View> <View style={styles.vertical_view}> <Image source={require('./img/iei.png')} style={{alignSelf:'center',height:45}}/> <Text style={styles.top_text} > 兴趣部落 </Text> </View> </View> <View style={{height:30,backgroundColor:'#f9f9fb'}}/> <View style={styles.rectangle_view}> <View style={{flexDirection:'row',alignItems: 'center'}}> <Image source={require('./img/nsa.png')} style={{alignSelf:'center',width:30,height:30}}/> <Text style={styles.rectangle_text} > 羽毛球 </Text> </View> <Image source={require('./img/ppe.png')} style={{alignSelf:'center',width:20,height:20}}/> </View> <View style={styles.rectangle_view}> <View style={{flexDirection:'row',alignItems: 'center'}}> <Image source={require('./img/nsb.png')} style={{alignSelf:'center',height:30}}/> <Text style={styles.rectangle_text} > 火车票 </Text> </View> <Image source={require('./img/ppe.png')} style={{alignSelf:'center',alignItems: 'center'}}> <Image source={require('./img/nrz.png')} style={{alignSelf:'center',height:30}}/> <Text style={styles.rectangle_text} > 视频 </Text> </View> <Image source={require('./img/ppe.png')} style={{alignSelf:'center',height:20}}/> </View> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1,backgroundColor: 'white',},title_view:{ flexDirection:'row',height:50,justifyContent: 'center',alignItems: 'center',backgroundColor:'#27b5ee',title_text:{ color:'white',fontSize:20,textAlign:'center' },three_image_view:{ paddingTop: 15,flexDirection:'row',justifyContent: 'space-around',backgroundColor:'white',vertical_view:{ justifyContent: 'center',paddingBottom:15,top_text:{ marginTop:5,color:'black',fontSize:16,rectangle_view:{ paddingTop:8,paddingBottom:8,paddingLeft:15,paddingRight:15,justifyContent: 'space-between',borderBottomColor:'#dedfe0',borderBottomWidth:1,rectangle_text:{ color:'black',textAlign:'center',paddingLeft:8,}); AppRegistry.registerComponent('ImageDemo',() => ImageDemo);
最后我想说,不懂的可以留言,或者去我的微信公众号下面留言,评论,一起交流学习。我的微信公众号:非著名程序员。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |