react native 中引用 自定义Image组件

  1. 创建自定义组件Image的Class类
  •  1 import React,{ Component} from 'react';
     2 import{
     3     View,
     4     Image,
     5     StyleSheet
     6 } from 'react-native'
     7 
     8 
     9 export default class LKImage extends Component{
    10     constructor(){
    11         super();
    12     }
    13 
    14     render(){
    15         return(
    16             <View>
    17             {/* 加载本地图片*/}
    18             <Image 
    19                 source={require('./../accets/images/mine.png')}
    20                 style={{width:200,height:200}}
    21             />
    22             {/* 加载网络图片*/}
    23             <Image 
    24                 source={{uri:'https://hrbrzyk.com/img/banner1.jpg'}}
    25                 style={{width:200,height:200}}
    26                 />
    27             </View>
    28         )
    29     }
    30 }

    注意:在通过uri 加载网络图片时候,必须要设置宽度和高度,否则显示不出来图片。通过 require('')加载本地图片时候可以不用设置宽度和高度。

  2. 在App.js中引用 自定一组件

 1 /**
 2  * Sample React Native App
 3  * https://github.com/facebook/react-native
 4  *
 5  * @format
 6  * @flow strict-local
 7  */
 8 
 9 import React from 'react';
10 import {
11   SafeAreaView,
12   StyleSheet,
13   ScrollView,
14   View,
15   Text,
16   StatusBar,
17 } from 'react-native';
18 
19 import {
20   Header,
21   LearnMoreLinks,
22   Colors,
23   DebugInstructions,
24   ReloadInstructions,
25 } from 'react-native/Libraries/NewAppScreen';
26 
27 import LKText from './components/LKText';
28 import LKImage from './components/LKImage';
29 
30 
31 const App: () => React$Node = () => {
32 
33   return (
34     <>
35       <StatusBar barStyle="dark-content" />
36       <SafeAreaView style={styles.mainViewStyle}>
37 
38       <LKImage/>
39       </SafeAreaView>
40     </>
41   );
42 };
43 
44 
45 const styles=StyleSheet.create({
46   mainViewStyle:{
47       flex:1,
48       backgroundColor:'red',
49   }
50 });
51 
52 
53 
54 export default App;

结果如图:

 

 

  

posted @ 2021-01-09 14:28  创客未来  阅读(256)  评论(0编辑  收藏  举报