1

I have a custom attribute of type RgbColor in my library, I wanted to know how can I initialise it to a specific color of my choice using ability_main.xml?

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108

1 Answers1

0

You can check the below example code: If this is the xml code with custom attribute (backgroundColor)

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

In CustomView.java, if backgroundColor value is being assigned to a RgbColor object, then you cantry this below code

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