首页 > 其他分享 >HarmonyOS 与 ArkTS | 自定义组件

HarmonyOS 与 ArkTS | 自定义组件

时间:2024-03-17 17:11:07浏览次数:16  
标签:10 ArkTS 自定义 width 100% isOn HarmonyOS png

HarmonyOS 与 ArkTS | 自定义组件

原效果:

代码:

@Entry
@Component
struct Index {
  build() {
    Column(){
      Row(){
        Column(){
          this.ItemCard($rawfile('ic_controlcenter_eyeconfort_filled.png'))
          Divider()
          .margin({top: 10,bottom: 10})
          this.ItemCard($rawfile('ic_controlcenter_nfc_filled.png'))
        }
      }
      .margin({top: 20})
      .width('80%')
      .height('150')
      .backgroundColor(Color.White)
      .borderRadius(10)
      .shadow({radius: 30,color: Color.Black})
    }
    .width('100%')
    .height('100%')
    .backgroundColor('#E7E8EB')

  }
  // 局部自定义构建函数
@Builder ItemCard(src: Resource){
  Row(){
    Image(src)
      .width('50')
      .interpolation(ImageInterpolation.High)// 图片插值函数,让图片更清晰
    Toggle({ type: ToggleType.Switch, isOn: false })
      .selectedColor('#007DFF')
      .switchPointColor('#FFFFFF')
      .onChange((isOn: boolean) => {
        console.info('Component status:' + isOn)
      })
  }
  .width('100%')
  .justifyContent(FlexAlign.SpaceEvenly)
}
}

如何用一种通用的方式为我们的页面添加顶栏?

答案是创建自定义组件,创建以下代码文件:

@Component
export struct Header{
  private title: ResourceStr
  build(){
    Row(){
      Image($rawfile('ic_public_arrow_left.png'))
        .width('30')
      Text(this.title)
        .fontSize('30')
        .fontWeight(FontWeight.Bold)
      Blank()
      Image($rawfile('ic_public_refresh.png'))
        .width('30')
    }
    .width('100%')
    .height('48')
  }
}

在刚才的页面文件中导入组件(通常会自动导入):import { Header } from '../components/CommonComponents'

然后在合适的位置调用组件即可。

项目结构:

最终效果:

代码:

import { Header } from '../components/CommonComponents'
@Entry
@Component
struct Index {
  build() {
    Column(){
      Header({title: '设置'})
      .backgroundColor(Color.White)
      Row(){
        Column(){
          this.ItemCard($rawfile('ic_controlcenter_eyeconfort_filled.png'))
          Divider()
          .margin({top: 10,bottom: 10})
          this.ItemCard($rawfile('ic_controlcenter_nfc_filled.png'))
        }
      }
      .margin({top: 20})
      .width('80%')
      .height('150')
      .backgroundColor(Color.White)
      .borderRadius(10)
      .shadow({radius: 30,color: Color.Black})
    }
    .width('100%')
    .height('100%')
    .backgroundColor('#E7E8EB')

  }
  // 局部自定义构建函数
@Builder ItemCard(src: Resource){
  Row(){
    Image(src)
      .width('50')
      .interpolation(ImageInterpolation.High)// 图片插值函数,让图片更清晰
    Toggle({ type: ToggleType.Switch, isOn: false })
      .selectedColor('#007DFF')
      .switchPointColor('#FFFFFF')
      .onChange((isOn: boolean) => {
        console.info('Component status:' + isOn)
      })
  }
  .width('100%')
  .justifyContent(FlexAlign.SpaceEvenly)
}
}

标签:10,ArkTS,自定义,width,100%,isOn,HarmonyOS,png
From: https://www.cnblogs.com/JayHsu/p/18078779

相关文章

  • HarmonyOS 与 ArkTS | ForEach 循环渲染 + List 实现滑动视频列表
    HarmonyOS与ArkTS|ForEach循环渲染+List实现滑动视频列表本文为记录,内容较简单,无注释。实现效果:代码:importimagefrom'@ohos.multimedia.image'classItem{name:stringclassification:stringimage:ResourceStrconstructor(name:string,classi......
  • 【WPF】自定义按钮样式(添加依赖属性、圆角)
    参考https://www.bilibili.com/video/BV13D4y1u7XX/?p=21代码示例1、自定义CustomButton按钮继承ButtonnamespaceWpfStudy.Buttons{publicclassCustomButton:Button{publicCornerRadiusCornerRadius{get{return(CornerRa......
  • 使用MQTT.fx和自定义Client(Ubuntu上实现)测试MQTT服务器(EMQX )
    目录概述1配置EMQX做MQTT服务器1.1登录EMQX1.2配置EMQX1.2.1配置客户端认证1.2.2创建用户2测试MQTT服务器2.1配置MQTT.fx工具2.2连接MQTT服务器3使用MQTT.fx发布和订阅信息3.1在MQTT.fx上发布信息3.2在MQTT.fx上订阅信息4Ubuntu上实现MQTTClient4......
  • 什么是C#的扩展方法,要怎么自定义使用
    介绍:C#中的扩展方法是一种特殊的静态方法,允许你向现有的类添加新的方法,而无需修改类的原始定义。它们使得可以在不修改原始类的情况下,给类添加新的行为。这种功能在编写库或者框架时非常有用,因为它允许你向已有的类添加新的功能,同时不会破坏现有的代码。原理:扩展方法......
  • Qt Charts 自定义样式
    QtCharts是Qt框架中用于创建图表和图形可视化的模块,它提供了一些内置的图表类型,如折线图、柱状图、饼图等。如果你想要自定义QtCharts中图表的样式,你可以使用一些方法来实现。以下是一些自定义QtCharts样式的方法:1、使用QChart的样式属性: QtCharts中的QChart类具有许多属性......
  • 鸿蒙Harmony应用开发—ArkTS声明式开发(容器组件:RelativeContainer)
    相对布局组件,用于复杂场景中元素对齐的布局。说明:该组件从APIVersion9开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。规则说明容器内子组件区分水平方向,垂直方向:水平方向为left,middle,right,对应容器的HorizontalAlign.Start,HorizontalAl......
  • 鸿蒙Harmony应用开发—ArkTS声明式开发(容器组件:Refresh)
    可以进行页面下拉操作并显示刷新动效的容器组件。说明:该组件从APIVersion8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。子组件支持单个子组件。从APIversion11开始,Refresh子组件会跟随手势下拉而下移。接口Refresh(value:RefreshOptio......
  • 鸿蒙Harmony应用开发—ArkTS声明式开发(容器组件:Panel)
    可滑动面板,提供一种轻量的内容展示窗口,方便在不同尺寸中切换。说明:该组件从APIVersion7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。子组件可以包含子组件。说明:子组件类型:系统组件和自定义组件,支持渲染控制类型(if/else、ForEach和LazyFo......
  • SOLIDWORKS参数化工具自定义设置小技巧 慧德敏学
    利用SOLIDWORKS参数化工具-SolidKits.AutoWorks,我们可以实现产品的一键改型、打包、编码、出BOM。整个流程大家都已经非常熟悉了,但是参数化工具的设置你可能还没有留心,加入你想自定义产品选型的分类名称,那在设置中就可以轻松实现。软件中,产品分类的默认名称为产品大类、产品小类......
  • vue 3+TS 封装自定义右键全局菜单(虚拟节点)
    有时我们需要点击(右键或左键)某个元素时弹出菜单,实现复制、粘贴、删除等功能。本文将介绍如何封装一个自定义的右键全局菜单(无三方依赖)。封装的菜单可自定义菜单项,图标,禁用,分割线,隐藏等。并且可以在全局任意地方使用。源码在文章末尾。效果使用<template><div>......