Codename One容器背景颜色

4
我正在尝试使用以下代码更改特定容器的背景颜色:
Container container = new Container(new BorderLayout());
container.getStyle().setBgColor(0x99CCCC);

但是没有任何反应,我也使用了repaint(),但仍然没有任何反应。同样的情况也出现在setBgTransparency(0)上。

如果您不想要透明度,那么应该使用 setBgTransparency(255) - NETCreator Hosting - WebDesign
4个回答

2
组件背景可能会有些棘手。以下是一些需要考虑的事项:
  1. 如果样式定义了图像边框,则该边框将优先于任何其他背景设置。
  2. 如果样式具有图像背景,则该背景将优先于 BgColor()。
  3. 如果样式的 BgTransparency() 设置为 0,则无论您设置什么 bgcolor,都看不到它。
因此,为了涵盖所有可能性,您可以执行以下操作:
myComponent.getAllStyles().setBorder(Border.createEmpty());
myComponent.getAllStyles().setBackgroundType(BACKGROUND_NONE);
myComponent.getAllStyles().setBgTransparency(255);
myComponent.getAllStyles().setBgColor(myColor);
或者,使用 ComponentSelector 类的流畅 API:
$(myComponent)
    .setBorder(Border.createEmpty())
    .setBackgroundType(BACKGROUND_NONE)
    .setBgTransparency(255)
    .setBgColor(myColor);

2
如果您想要格式化容器或更改容器的样式,那么您只需要在设计器中为容器创建UIID。在这里,您可以格式化背景颜色、边距、填充等。因此,您只需要创建UIID并将其应用于特定容器即可。
例如:
Container container = new Container();
container.setUIID("Container_uiid_name");

并且您可以获得预期的输出。


欢迎,很高兴能为您提供帮助? - Gaurav Takte

2

将setBgTransparency(0)设置为容器透明,因此将setBgTransparency设置为255以使其不透明。希望以下代码能帮助您

Container container = new Container(new BorderLayout());
container.getStyle().setBgColor(0x99CCCC);
container.getStyle().setBgTransparency(255);

1
在CodeNameOne中,有三个步骤可以创建一个渐变颜色的容器: 1. 获取未选样式 2. 设置背景类型: 可以是以下任意一种: BACKGROUND_GRADIENT_LINEAR_HORIZONTAL BACKGROUND_GRADIENT_LINEAR_VERTICAL...
  1. setBackgroundGradientStartColor and EndColor (if you wish to have no gradient you should make same color for StartColor and EndColor)

                Container Container1 = new Container();
                Container1.getUnselectedStyle().setBackgroundType(Style.BACKGROUND_GRADIENT_RADIAL);
                Container1.getUnselectedStyle().setBackgroundGradientEndColor(0xFFBCCA);
                Container1.getUnselectedStyle().setBackgroundGradientStartColor(0xFFBCCA);
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接