React Native 图片组件基础知识

在 React Native 中使用图片其实跟 HTML 中使用图片一样简单,在 React Native 中我们使用Image组件来呈现图片的内容,其中主要的属性有:source。这个属性主要是设置图片的内容,它可以是网络图像地址、静态资源、临时本地图像以及本地磁盘中的图像。

制作一个图片案例

搭建基础框架

typescript 复制代码
export default function ImageCard() {
  return (
    <View style={[styles.card, styles.cardElevated]}>
      <Image
        style={styles.cardImage}
        source={{
          uri: "https://cdn.pixabay.com/photo/2020/05/05/17/49/tree-5134167_960_720.jpg",
        }}
      />
      <View style={styles.cardBody}>
        <Text style={styles.cardTitle}>
          风景摄影是对摄影师的最高考验------而且往往是最令人失望的。
        </Text>
        <Text style={styles.cardLabel}>风景</Text>
        <Text style={styles.cardDescription}>
          自从年轻的安塞尔·亚当斯 (Ansel Adams) 于 1916
          年在优胜美地国家公园度假时拍摄了他的第一张照片(使用他父亲送给他的柯达布朗尼相机)以来,摄影师们一直试图记录我们星球的无限美丽和威严。
          为了庆祝我们的 2022风景摄影奖,我们调查了我们之前奖项的一些最佳提交和
          selected 10 幅令人惊叹的风景图像,展示了该类型的巨大潜力。
        </Text>
        <Text style={styles.cardFooter}>2023.08.13</Text>
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  card: {},
  cardElevated: {},
  cardImage: {},
  cardBody: {},
  cardTitle: {},
  cardLabel: {},
  cardDescription: {},
  cardFooter: {},
});

编写卡片样式

typescript 复制代码
card: {
  borderRadius: 8,
  marginHorizontal: 12,
  marginVertical: 16
},
cardElevated: {
  backgroundColor: '#FFFFFF',
  elevation: 3,
  shadowOffset: {
    width: 1,
    height: 1
  }
},

编辑卡片内容相关样式

typescript 复制代码
cardBody: {
  paddingHorizontal: 12
},
cardTitle: {
  color: '#000000',

},
cardLabel: {
  color: '#000000',
  marginTop: 6
},
cardDescription: {
  color: '#000000',
  fontSize: 12,
  marginBottom: 12,
  marginTop: 6,
  flexShrink: 1,
  lineHeight: 22,
  textAlign: 'justify'
},
cardFooter: {
  color: '#000000',
  fontSize: 12,
  marginBottom: 8
}

编辑图片样式

typescript 复制代码
cardImage: {
  height: 180,
  marginBottom: 8,
  borderTopLeftRadius: 6,
  borderTopRightRadius: 6
},

运行效果如下

相关推荐
羊群智妍7 小时前
2026 AI搜索流量密码:免费GEO监测工具,优化效果看得见
笔记·百度·微信·facebook·新浪微博
阿蒙Amon8 小时前
TypeScript学习-第10章:模块与命名空间
学习·ubuntu·typescript
AI绘画哇哒哒8 小时前
【干货收藏】深度解析AI Agent框架:设计原理+主流选型+项目实操,一站式学习指南
人工智能·学习·ai·程序员·大模型·产品经理·转行
戌中横9 小时前
JavaScript——预解析
前端·javascript·学习
●VON9 小时前
React Native for OpenHarmony:2048 小游戏的开发与跨平台适配实践
javascript·学习·react native·react.js·von
山岚的运维笔记9 小时前
SQL Server笔记 -- 第18章:Views
数据库·笔记·sql·microsoft·sqlserver
ZH15455891319 小时前
Flutter for OpenHarmony Python学习助手实战:自动化脚本开发的实现
python·学习·flutter
xcLeigh10 小时前
Python入门:Python3 requests模块全面学习教程
开发语言·python·学习·模块·python3·requests
xcLeigh10 小时前
Python入门:Python3 statistics模块全面学习教程
开发语言·python·学习·模块·python3·statistics
GHL28427109010 小时前
分析式AI学习
人工智能·学习·ai编程