首页 > 解决方案 > 如何从ability_main.xml 初始化 RgbColor?

问题描述

我的库中有一个 RgbColor 类型的自定义属性,我想知道如何使用ability_main.xml 将其初始化为我选择的特定颜色?

标签: javaandroidhuawei-mobile-serviceshuawei-developersharmonyos

解决方案


您可以查看以下示例代码:如果这是具有自定义属性(背景颜色)的 xml 代码

 <com.example.application.CustomView
        ohos:width="300vp"
        ohos:height="100vp"
        app:backgroundColor="#FF000000">

在 CustomView.java 中,如果 backgroundColor 值被分配给 RgbColor 对象,那么你可以试试下面的代码

if (attrs.getAttr(BACKGROUND_COLOR).isPresent() && attrs.getAttr(BACKGROUND_COLOR).get() != null) {
            mBackgroundColor = new RgbColor(attrs.getAttr(BACKGROUND_COLOR).get().getIntegerValue());
}

推荐阅读