react native 中引用 自定义Image组件
- 创建自定义组件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;
结果如图:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!