【问题标题】:What is the alternative in Harmony OS for function setBackgroundColor() on View class in Android?Harmony OS 在 Android 的 View 类上的函数 setBackgroundColor() 的替代方法是什么?
【发布时间】:2021-07-30 09:28:33
【问题描述】:
我正在开发一个 HarmonyOS 项目,我想设置组件的背景颜色。在Android中我们在View类中有一个函数setBackgroundColor(),如下图所示。
View titleLayout = view.findViewById(R.id.titleLayout);
titleLayout.setBackgroundColor(calendarTitleBackgroundColor);
如何在 HarmonyOS 中为组件设置背景颜色?
【问题讨论】:
标签:
java
android
huawei-mobile-services
huawei-developers
harmonyos
【解决方案1】:
@Gowtham GS的答案是正确的。我想补充一点:
在XML文件中定义组件时也可以定义组件的背景颜色。属性为background_element。
例如:ohos:background_element="white"
【解决方案2】:
首先你必须使用某种颜色构建一个元素,
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()));