我尝试使用BackgroundPainter通过代码自定义某些UI,但我遇到奇怪和意外的结果:
from collections import defaultdict
d = """1 A 10
2 A 20
1 B 30
1 C 40
2 B 50""".split("\n")
d = [i.split(" ") for i in d]
# Default value is {1: "0", 2: "0"}
data = defaultdict(lambda: dict(set([(i[0], "0") for i in d])))
# Update values for 1 and 2
for idx, name, val in d:
data[name][idx] = val
# (Missing the colum ids, but could be added)
print ["{} {} {}".format(*([k] + v.values())) for k, v in sorted(data.items())]
>>> ['A 10 20', 'B 30 50', 'C 40 0']
结果,我可以看到"组件"描绘自己的黑色背景,而南方"不会(它应该是半红色)。我做错了吗?此外,GridLayout没有正确处理边距/填充,我必须增加标签填充以避免它被剪切。
感谢。
答案 0 :(得分:2)
我在过去遇到过类似的问题,不得不抛弃BackgroundPainter制作2种颜色。
从你发布的代码中,可以通过以下方式实现类似的风格:
只需复制并粘贴它就可以了,你也可以替换component
上的BP来使用添加的方法
public class TabView extends Form {
private Tabs tabs;
public TabView() {
super("Radio Web", new BorderLayout());
tabs = new Tabs(Component.BOTTOM);
addComponent(BorderLayout.CENTER, tabs);
tabs.setSwipeActivated(false);
Component component = null;
Button playButton = new Button(FontImage.createMaterial(FontImage.MATERIAL_PLAY_ARROW, UIManager.getInstance().getComponentStyle("Title"), 10));
playButton.getAllStyles().setBorder(RoundBorder.create().color(0x94170C));
playButton.getAllStyles().setMarginBottom(0);
playButton.getAllStyles().setMarginLeft(0);
playButton.getAllStyles().setMarginTop(0);
Label mediaInfoLabel = new Label("Artist - Title");
mediaInfoLabel.getAllStyles().setPaddingTop(2);
mediaInfoLabel.getAllStyles().setPaddingBottom(2);
Slider volumeSlider = new Slider();
volumeSlider.setMinValue(0);
volumeSlider.setMaxValue(100);
volumeSlider.setEditable(true);
Container box = GridLayout.encloseIn(1, new Container(), volumeSlider, mediaInfoLabel);
Container south = new Container(new FlowLayout(Component.LEFT, Component.BOTTOM));
south.add(playButton).add(box);
paintBackgroundRect2Colors(south);
component = BorderLayout.south(south);
component.getAllStyles().setBgPainter(new BackgroundPainter(component) {
@Override
public void paint(Graphics g, Rectangle rect) {
int color = g.getColor();
int alpha = g.getAlpha();
g.setColor(0x150100);
g.setAlpha(255);
g.fillRect(0, 0, rect.getWidth(), rect.getHeight());
g.setColor(color);
g.setAlpha(alpha);
}
});
tabs.addTab("", FontImage.MATERIAL_AUDIOTRACK, 5, component);
BrowserComponent sito = new BrowserComponent();
sito.setURL("https://www.codenameone.com");
tabs.addTab("", FontImage.MATERIAL_HTTP, 5, sito);
BrowserComponent fb = new BrowserComponent();
fb.setURL("https://www.facebook.com");
tabs.addTab("", FontImage.MATERIAL_FACE, 5, fb);
}
public static void paintBackgroundRect2Colors(Component cmp) {
Image i = Image.createImage(cmp.getPreferredW(), cmp.getPreferredH(), 0xffffff);
Graphics g = i.getGraphics();
g.setAntiAliased(true);
boolean antiAliased = g.isAntiAliased();
int y = i.getHeight() / 2;
// Top half
g.setAlpha(255); //Change this to 0 if you want top half to be transparent
g.setColor(0x150100); // Change this to any color you want, I used "component" color just to blend the look and feel
g.fillRect(0, 0, i.getWidth(), y);
// Bottom half
g.setAlpha(255);
g.setColor(0xD12115);
g.fillRect(0, y, i.getWidth(), i.getHeight());
g.setAntiAliased(antiAliased);
// Set background image and make sure it fills
cmp.getAllStyles().setBgImage(i, true);
cmp.getAllStyles().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FILL, true);
}
}