我正在处理一个HarmonyOS项目,因为我想设置组件的背景色。在Android中,我们在视图类中有一个函数setBackgroundColor(),如下所示。
View titleLayout = view.findViewById(R.id.titleLayout);
titleLayout.setBackgroundColor(calendarTitleBackgroundColor);如何在HarmonyOS中为组件设置背景色?
发布于 2021-07-30 09:51:00
首先,你必须使用一些颜色来构建一个元素,
public static Element buildElementByColor(int color) {
ShapeElement drawable = new ShapeElement();
drawable.setShape(ShapeElement.RECTANGLE);
drawable.setRgbColor(RgbColor.fromArgbInt(color));
return drawable;
}稍后,您将使用setBackground应用编程接口设置构建的元素
component.setBackground(buildElementByColor((Color.DKGRAY).getValue()));发布于 2021-08-02 08:28:54
@Gowtham GS的回答是正确的,我想补充一点:
在XML文件中定义组件时,还可以定义组件的背景色。该属性为background_element。
例如:ohos:background_element="white"
https://stackoverflow.com/questions/68588609
复制相似问题