我正在构建一个 HarmonyOS 应用程序,并希望加载我放在资源文件夹中的颜色值resources/base/element/color.json
。如何在我的 Java 类中加载这种颜色?
在 Android 中,我们可以使用该getColor()
函数:
context.getResources().getColor(R.color.colorID);
在 HarmonyOS 中有什么替代方案?
我正在构建一个 HarmonyOS 应用程序,并希望加载我放在资源文件夹中的颜色值resources/base/element/color.json
。如何在我的 Java 类中加载这种颜色?
在 Android 中,我们可以使用该getColor()
函数:
context.getResources().getColor(R.color.colorID);
在 HarmonyOS 中有什么替代方案?
可以参考以下实现:</p>
{
"color": [
{
"name": "primary",
"value": "#FF0000"
}
]
}
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
ResourceManager resManager = this.getResourceManager();
try {
int color = resManager.getElement(ResourceTable.Color_primary).getColor();
Text text = (Text) findComponentById(ResourceTable.Id_text_helloworld);
text.setTextColor(new Color(color));
} catch (IOException e) {
} catch (NotExistException e) {
} catch (WrongTypeException e) {
}
}
</p>