开发者

How to wrap labels and left align them to the right

I have this code

HorizontalPanel hp = new HorizontalPanel();
hp.setWith("100%");
hp.setHorizontalAlignment(HasHorizontalAlignme开发者_运维技巧nt.ALIGN_LEFT);
hp.add(new Label("label 1"));
hp.add(new Anchor("anchor 1"));
hp.add(new Label("label 2"));
hp.add(new Anchor("Anchor 2"));
RootPanel.get().add(hp);

Actually it displays

label1...........................................anchor1......................................label2..................................anchor2

I actually wanted something like

label1 anchor1 label2 anchor2

Is there a layout for that or other solution ? thx


You can keep your HorizontalPanel, but try this:

HorizontalPanel hp = new HorizontalPanel();
hp.setHorizontalAlignment(HasAlignment.ALIGN_LEFT);
hp.setSpacing(2); // 2 = number of pixels between each item
hp.add(new Label("label 1"));
hp.add(new Anchor("anchor 1"));
hp.add(new Label("label 2"));
hp.add(new Anchor("Anchor 2"));
RootPanel.get().add(hp);

All you really needed was a setSpacing property so they're not literally attached to the next cell in your hp, and to remove the hp.setWidth("100%"). If you were doing this in UiBinder, you could keep the 100% width and set the width of each <g:cell> independently, allowing more flexibility.

Also, I suggest you use HasAlignment over HasHorizontalAlignment and/or HasVerticalAlignment, if for no other reason than HasAlignment is portable to all types of panels.


Try FlowPanel. It simply adds widgets from left to right, no spacing, and wraps at the end of the line.

Maybe I'm missing something. It there something more to the layout that you want?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜