按比例分割屏幕LWUIT

问题描述:

我想垂直分割屏幕30%和70%,我怎样才能达到这与lwuit?我使用/尝试GridLayout,但它平分屏幕。需要一个示例代码。按比例分割屏幕LWUIT

在此先感谢!

旋转设备的屏幕时,其他答案都会失败。

您可以采取两种方法,使用支持布局约束的百分比分布的表格布局。

或者创建一个覆盖calcPreferredSize方法的Contaienr的子类,并适当返回30或70%的维数。然后,只需将它们两个都加入到BoxLayout容器中并按需要使用,例如:

Container c30 = new Container() { 
     public Dimension calcPreferredSize() { 
      new Dimension(Display.getInstance().getPreferredHeight(), (int)(Display.getInstance().getPreferredWidth() * 0.7)); 
     } 
}; 

创建其派生的容器类:

public class split extends Container { 
    public split(int h) 
    { 
     super(); // you can set your layout type here 
     setPreferredH(h); 
    } 
} 

然后加入这一类的成分在你的表格:

public class e extends Form { 
    private Container c1, c2; 
    private TextField f1,f2; 
    public e() 
    { 
     super("test split"); 
     c1 = new split(30*getPreferredH()/100); 
     c2 = new split(70*getPreferredH()/100); 
     f1 = new TextField("ghgjhg"); 
     f2 = new TextField("jkdhuhg"); 
     c1.addComponent(f1); 
     c2.addComponent(f2); 
     setLayout(new BoxLayout(BoxLayout.Y_AXIS)); 
     addComponent(c1); 
     addComponent(c2); 
    } 
} 

你甚至可以设置一个backgroundPainter的分班直观地显示出分裂。