1

我正在开发一个 HarmonyOS 项目,因为我想设置组件的背景颜色。在 Android 中,我们setBackgroundColor()在 View 类中有一个函数,可以如下所示完成。

View titleLayout = view.findViewById(R.id.titleLayout);
titleLayout.setBackgroundColor(calendarTitleBackgroundColor);

如何为 HarmonyOS 中的组件设置背景颜色?

4

2 回答 2

1

@Gowtham GS的回答是正确的。我想补充一点:

您还可以在 XML 文件中定义组件时定义组件的背景颜色。属性是background_element

例如:ohos:background_element="white"

于 2021-08-02T08:28:53.677 回答
0

首先,您必须使用某种颜色构建一个元素,

    public static Element buildElementByColor(int color) {
        ShapeElement drawable = new ShapeElement();
        drawable.setShape(ShapeElement.RECTANGLE);
        drawable.setRgbColor(RgbColor.fromArgbInt(color));
        return drawable;
    }

稍后您使用 setBackground API 设置了构建的元素

    component.setBackground(buildElementByColor((Color.DKGRAY).getValue()));
于 2021-07-30T09:51:00.143 回答