微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当我在 xml 文件中使用带有值的单元即 vp、fp、px时,相应的属性不起作用? (和谐操作系统)

如何解决当我在 xml 文件中使用带有值的单元即 vp、fp、px时,相应的属性不起作用? (和谐操作系统)

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

例如:

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

在我的自定义组件类中,我得到这样的属性

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

解决方法

我们只需要替换

getIntegerValue()getDimensionValue()

例如:

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

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

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

看下面的例子:

.xml:

struct ContentView: View {
    var text = AttributedString("**Hello**,`world`! Visit our [website](https://www.capitalone.com).")

    var body: some View {
        VStack {
            Text("**Hello**,`world`! Visit our [website](https://www.capitalone.com).")
                .padding()

            Text(text)
                .padding()
        }
    }
}

.java:

<?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"

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。