官术网_书友最值得收藏!

Using CSS layouts for mobile devices

Another nice feature of the CSS layout is that components are wrapped when they reach the width of the layout. This feature can be used to create layouts for small displays, for example, mobile phones or some tablets. We will create a simple layout with a header, two menus, and content in the middle of them.

As we can see in the following screenshot, if the user opens our application on a wide screen, components are displayed side by side. Except the header that takes up the whole width of the page.

Using CSS layouts for mobile devices

If the user opens our application on a narrow screen, for example, on a mobile device, then all components will be aligned into the one column.

Using CSS layouts for mobile devices

How to do it...

Carry out the following steps to create an application with a flexible layout for mobile devices:

  1. Create a project with the main UI class called, for example, Demo.
    public class Demo extends UI {…}
  2. We create a MobileLayout class that extends CssLayout.
    public class MobileLayout extends CssLayout {…}
  3. At first we create a constant with Lorem Ipsum text.
    private static final String LIPSUM = 
      "Lorem ipsum dolor sit amet, consectetur adipisicing elit.";
  4. The key functionality is in the constructor. Here we create all the sections of the layout. On the top of the page we put the header. On the left and right sides we insert menus. In the middle will be the content of the page.
    public MobileLayout() { 
      Label header = 
      new Label("<h1>CSS layout</h1>", ContentMode.HTML);
      addComponent(header);
      
      addComponent(createMenu()); 
      
      Label content = new Label(LIPSUM);  
      content.setWidth(70, Unit.PERCENTAGE);  
      addComponent(content);
      
      addComponent(createMenu()); 
    }
  5. For a better look, we set a margin around all components. We also align them vertically upwards. As in the previous recipe, we can do it by overriding the getCss() method.
    @Override
    protected String getCss(Component c) {
      return "margin: 5px; vertical-align: top;";
    }
  6. Implementation of the createMenu() method is the same as in the Creating an adjustable layout using split panels recipe of this chapter.
    private Tree createMenu() {…}
  7. That's all. Now we can use or create MobileLayout in the main UI class called Demo.
    public class Demo extends UI {
      @Override
      protected void init(VaadinRequest request) {
        setContent(new MobileLayout());
      }
    }

We can run the server and open the application in the web browser.

How it works...

All the components in the CssLayout class are inserted horizontally and wrapped when they reach the width of the layout. Except the header that takes up the whole width of the page, because Label has a default setting of 100 percent width.

CssLayout has a very simple Document Object Model (DOM) structure. It's the fastest of the layout components.

See also

  • The Controlling components over the CSS layout recipe
主站蜘蛛池模板: 贵南县| 栾城县| 沁阳市| 玛纳斯县| 疏勒县| 玛多县| 泸溪县| 鄂州市| 哈尔滨市| 永城市| 黄山市| 岢岚县| 鹤岗市| 祁连县| 重庆市| 富平县| 海丰县| 肃南| 阿坝县| 辽源市| 师宗县| 秦皇岛市| 阳山县| 宜宾市| 游戏| 化州市| 色达县| SHOW| 金山区| 蓬安县| 越西县| 荥经县| 百色市| 连州市| 白沙| 义乌市| 崇左市| 福泉市| 镇康县| 尉氏县| 东乡族自治县|