鸿蒙5.0开发进阶:API ArkUI框架-ArkTS组件(前景色设置)

往期鸿蒙全套实战文章必看:(文中附带全栈鸿蒙学习资料)


前景色设置

设置组件的前景色。

说明

从API Version 10开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。

foregroundColor

foregroundColor(value: ResourceColor | ColoringStrategy)

设置组件的前景色。当组件未设置前景色,默认继承父组件。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
valueResourceColor | ColoringStrategy设置组件的前景颜色或者根据智能取色策略设置前景颜色。不支持属性动画。

示例

示例1(使用前景色设置)

该示例主要演示通过foregroundColor设置前置景色。

// xxx.ets
@Entry
@Component
struct ForegroundColorExample {
  build() {
    Column({ space: 100 }) {
      // 绘制一个直径为150的圆,默认填充色为黑色
      Circle({ width: 150, height: 200 })
      // 绘制一个直径为150的圆,
      Circle({ width: 150, height: 200 }).foregroundColor(Color.Red)
    }.width('100%').backgroundColor(Color.Blue)
  }
}

示例2(设置前景色为组件背景色反色)

该示例通过ColoringStrategy.INVERT将前置景色设置为背景色反色。

// xxx.ets
@Entry
@Component
struct ColoringStrategyExample {
  build() {
    Column({ space: 100 }) {
      // 绘制一个直径为150的圆,默认填充色为黑色
      Circle({ width: 150, height: 200 })
      // 绘制一个直径为150的圆,设置前景色为组件背景色的反色
      Circle({ width: 150, height: 200 })
        .backgroundColor(Color.Black)
        .foregroundColor(ColoringStrategy.INVERT)
    }.width('100%')
  }
}

示例3(前置景色未继承父组件)

该示例主要演示组件同时设置前置景色和背景色与只设置背景色的效果对比。

// xxx.ets
@Entry
@Component
struct foregroundColorInherit {
  build() {
    Column() {
      Button('设置前景色为橘色').fontSize(20).foregroundColor(Color.Orange).backgroundColor(Color.Gray)
      Divider()
      Button('未设置前景色继承自父组件').fontSize(20).backgroundColor(Color.Gray)
    }.foregroundColor(Color.Red)
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值