问题描述
我正在使用 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"