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?
Asked
Active
Viewed 28 times
1

zhangxaochen
- 32,744
- 15
- 77
- 108

Shrey Shrivastava
- 51
- 3
-
hi@Shrey Shrivastava, Could you pls post some code of your library for us to locate your issue? – zhangxaochen Aug 03 '21 at 09:56
1 Answers
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