1

我正在构建一个 HarmonyOS 应用程序,并希望加载我放在资源文件夹中的颜色值resources/base/element/color.json。如何在我的 Java 类中加载这种颜色?

在 Android 中,我们可以使用该getColor()函数:

context.getResources().getColor(R.color.colorID);

在 HarmonyOS 中有什么替代方案?

4

1 回答 1

0

可以参考以下实现:</p>

  1. 颜色.json
{

  "color": [

    {

      "name": "primary",

      "value": "#FF0000"

    }

  ]

}

  1. MainAbilitySlice
    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>

于 2021-07-28T08:02:52.000 回答