0

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);
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
C2C
  • 85
  • 6

2 Answers2

1

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);
C2C
  • 85
  • 6
0

See the following example:

.xml:

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    xmlns:xxxx="http://schemas.huawei.com/res/ohos-auto"
    ...
    xxxx:iconMargin="8"

.java:

if (attrSet.getAttr("iconMargin").isPresent()) {
    int iconMargin = AttrHelper.vp2px(attrSet.getAttr("iconMargin").get().getIntegerValue(), context);
}
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
  • Yes, If I put any value without any **unit** then it's work properly, but whenever I put value with **unit(i.e. vp, fp, px)** then it's not working. – C2C Jul 30 '21 at 05:30