鸿蒙开发系列教程(二十六)--样式处理(二)

本文介绍了HarmonyOS中的图标库下载、图片资源的使用方法,包括本地图片、资源文件夹和网络图片的引用,以及网络图片的权限要求。此外,文章还详细讲解了样式复用的两种方式(@Styles和@Extends),以及如何通过封装原生组件来增强功能。
摘要由CSDN通过智能技术生成

1、图标库

官网:https://developer.harmonyos.com/cn/design/harmonyos-icon/

在这里插入图片描述

可以下载不同类型
在这里插入图片描述

2、图片资源

(1)image本地图片
图片放到放置到ets目录下-可创建文件夹:如assets文件
在这里插入图片描述
引用: Image(‘/assets/abc.png’).width(80).height(80)

(2)Resource\media下图片
放到resource\media目录下
在这里插入图片描述
引用:Image($r(‘app.media.a6’)).width(80).height(80)

(3)Resource\rawfile下图片
在这里插入图片描述
引用:Image($rawfile(“a8.jpeg”)).width(80).height(80)

(4)网络图片

Image(“https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png”).width(80).height(80)

重点:网络图片,在preview中时,可以预览
但在模拟器和真实环境中,必须申请网络权限

网络权限:moddule.json5
{
“module”: {
“name”: “entry”,

“requestPermissions”: [{
“name”:“ohos.permission.INTERNET”
}]
}
}

3、样式@Styles

@Styles 可以进行样式复用,解决在开发过程中会出现大量代码重复样式设置问题

全局定义:

**@Styles function textStyle () {
  .width(100)
  .height(150)
  .backgroundColor(Color.Yellow)
  .borderRadius(25)
}**

@Entry
@Component
struct StyleRepeat {
  build() {
    Row() {
      Column({ space: 15 }) {
        Text("测试11")
          .textStyle()
          .textAlign(TextAlign.Center)
        Text("测试22")
          .textStyle()
          .textAlign(TextAlign.Center)
      }
      .width('100%')
    }
    .height('100%')
  }
}

局部定义:

@Entry
@Component
struct StyleRepeat {
  **@Styles
  textStyle () {
    .width(200)
    .height(50)
    .backgroundColor(Color.Pink)
    .borderRadius(25)
   
  }**
  build() {
    Row() {
      Column({ space: 15 }) {
        Text("测试11")
          .textStyle()
          .textAlign(TextAlign.Center)
        Text("测试22")
          .textStyle()
          .textAlign(TextAlign.Center)
      }
      .width('100%')
    }
    .height('100%')
  }
}

4、样式@Extends

使用Extend来修饰一个全局的方法
一般对原生组件进行封装

@Entry
@Component
struct ExtendCase {
  build() {
    Row() {
      Column({ space: 20 }) {
        Text("测试")
          .textStyle()
        Button("按钮")
          .btnStyle()
        
      }
      .width('100%')
    }
    .height('100%')
  }
}

@Extend(Text) function textStyle () {
  .width(120)
  .height(160)
  .borderRadius(30)
  .backgroundColor(Color.Red)
  .textAlign(TextAlign.Center)
  .fontSize(20)
}
@Extend(Button) function btnStyle () {
  .width(120)
  .height(60)
  .borderRadius(30)
  .backgroundColor(Color.Pink)
  .fontSize(20)
}

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值