Search code examples
javaandroidhuawei-mobile-serviceshuawei-developersharmonyos

when I use unit(i.e. vp, fp, px) with value in xml file then respective attribute not working ? (HarmonyOS)


I am creating a custom component in HarmonyOS using Java SDK, where I created some attributes for my custom component. Now the problem is "whenever I am trying to set any value in attribute with unit(i.e. vp, fp, px)" then, Respective attribute is not working.

For ex:

ohos:iconMargin="8vp"
ohos:text_size="12fp"
ohos:areaMargin="24px"

And in my custom component class I get this attribute value like this

attr = attrSet.getAttr(areaMargin);
areaMargin = attr.map(Attr::getIntegerValue).orElse(24);

Solution

  • we have to just replace

    getIntegerValue() with getDimensionValue()

    for ex:

    attr = attrSet.getAttr(areaMargin);
    areaMargin = attr.map(Attr::getIntegerValue).orElse(24);
    

    Replace above code with below code

    attr = attrSet.getAttr(areaMargin);
    areaMargin = attr.map(Attr::getDimensionValue).orElse(24);