Lwuit布局管理

布局管理器中一个比较特殊的管理器[color=green]CoordinateLayout[/color]以坐标的形式给容器内的控件指
定一个绝对的位置,虽然[color=green]CoordinateLayout[/color]允许我们以x/y坐标的形式放置容器内的控件,但是它并不能保证控件的位置不会改变并且它也不能决定控件的绝对位置。

[color=green]CoordinateLayout[/color]只是“相对地”接收控件的位置,并根据接收到的位置计算出控件应该放置的位置。[color=green]CoordinateLayout[/color]在程序运行时,容器的有效空间总是随着字体的大小改变、屏幕的旋转而变化的。

[img]http://dl.iteye.com/upload/attachment/244210/8db7e43c-9c47-3bdd-a89b-8592c8b1df9f.jpg[/img]

容器中控件的坐标是由Lwuit计算出的,控件的尺寸并不要他们的[color=darkblue]setWidth[/color]和[color=darkblue]getHeight[/color]方法决定,而是由Lwuit最优调整计算得来的,它们的[color=blue]setWidth[/color]和[color=blue]getHeight[/color]方法被忽略。对于大小固定的空间可以运用[color=blue]setPreferredW[/color]和[color=blue]setPreferredH[/color]设置为尺寸不变;可以使用控件的[color=blue]setAlignment[/color]方法设置其在容器中的对齐方式。


Display.init(this);
mainForm = new Form("CoordinateLayout");
mainForm.setLayout(new CoordinateLayout(200,200));

Label centerLabel = new Label("Center");
centerLabel.setX(90);
centerLabel.setY(90);
centerLabel.getUnselectedStyle().setBgTransparency(100);
centerLabel.getUnselectedStyle().setBgColor(0xff);

Label underCenter = new Label("Under Center");
underCenter.setX(70); //设置控件的起始位置
underCenter.setY(110);

Label top = new Label("Top Left");
top.setAlignment(Component.CENTER);
top.setX(0);
top.setY(0);
top.setPreferredW(200); //设置控件的款高度
top.setPreferredH(30);
top.getUnselectedStyle().setBgColor(0x00ff00);

mainForm.addComponent(underCenter);
mainForm.addComponent(centerLabel);
mainForm.addComponent(top);
mainForm.show();



TableLayout的容器添加控件的时候要指明BorderLayout对象的约束,例如:container.addComponent(tableConstraint,component);但这个约束可以省略,不像BorderLayout那样是必须的。

TableLayout.Constaint类的一个实例,并且只能使用一次,重复使用此实例就会抛异常,就是说在使用了TableLayout的容器中,TableLayout.Constaint实例只能对一个控件有效,其他控件必须另行创建TableLayout.Constaint实例。TableLayout.Constaint可以用来指定某一行的高度和宽度。

TableLayout会自动根据行数和列数尽量多地空间分配给控件来使得控件尺寸最优,当空间不足时,新添加进来的空间就会“挤在一块”.

[color=red]注意:TableLayout中的控件尺寸默认设置为充满整个单元,可以调用Component或者Style类的相关方法来改变每个单元的对齐和填充方式。[/color]

[img]http://dl.iteye.com/upload/attachment/244235/246ab17d-973f-38ad-8f55-c21b6bec5785.jpg[/img]


Display.init(this);

try{
Resources res = Resources.open("/javaTheme.res");
UIManager.getInstance().setThemeProps(res.getTheme("javaTheme"));
}catch(Exception e){
e.printStackTrace();
}

mainForm = new Form("Table Layout");
TableLayout layout = new TableLayout(4,3);
mainForm.setLayout(layout);

TableLayout.Constraint constraint = layout.createConstraint();
constraint.setVerticalSpan(2); //设置第一个控件跨越2个单元
constraint.setWidthPercentage(50); //设置宽度为50%
mainForm.addComponent(constraint, new Label("First"));
mainForm.addComponent(new Label("Send"));
mainForm.addComponent(new Label("Third"));

constraint = layout.createConstraint();
constraint.setHeightPercentage(20); //设置长度度为20%
mainForm.addComponent(constraint, new Label("Fourth"));
mainForm.addComponent(new Label("Fifth"));

constraint = layout.createConstraint();
constraint.setHorizontalSpan(3); //设置第一个控件跨越3个单元
Label span = new Label("Spanning");
span.getStyle().setBorder(Border.createLineBorder(2));
span.setAlignment(Component.CENTER);

mainForm.addComponent(constraint,span);
mainForm.show();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值