当我在 xml 文件中使用带有值的单位(即 vp、fp、px)时,相应的属性不起作用? (鸿蒙)

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

我正在使用 Java SDK 在 HarmonyOS 中创建一个自定义组件,我在其中为我的自定义组件创建了一些属性。 现在的问题是“每当我尝试使用单位(即 vp、fp、px)设置属性中的任何值时”,相应的属性将不起作用。

例如:

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

在我的自定义组件中 class 我得到的属性值是这样的

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

参见以下示例:

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

我们只需要更换

getIntegerValue()getDimensionValue()

例如:

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

用下面的代码替换上面的代码

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