applibgroup / HarmonyOS-Knowledgebase

This repository contains code samples of HarmonyOS Training
Apache License 2.0
17 stars 6 forks source link

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

Closed Click2cloud-Eros closed 3 years ago

Click2cloud-Eros commented 3 years ago

Description

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);

Stackoverflow link here:

https://stackoverflow.com/questions/68558559/when-i-use-uniti-e-vp-fp-px-with-value-in-xml-file-then-respective-attribut

Additional information

Developer Platform: Windows DevEco Studio version: 2.1 Beta 4 SDK API version: 5

kanaksony commented 3 years ago

Replacing getIntegerValue() with getDimensionValue() should help you.