在使用GridLayout时,同一种设置常常有不同的写法,现总结如下:

1.GridLayout设置:

   GridLayout layout = new GridLayout();
   // 设置容器的列数
   layout.numColumns = 4;
   // 设置容器上的组件是否等距分开
   layout.makeColumnsEqualWidth = false;

   等价写法:
   GridLayout layout = new GridLayout(4, false);
 
2.GridData.grabExcessHorizontalSpace、GridData.grabExcessVerticalSpace
   ①GridData.grabExcessHorizontalSpace
       GridData gridData = new GridData();
       // 当容器大小变化时,组件所在列空间横向随容器的变化而变化
       gridData.grabExcessHorizontalSpace = true;
  
       等价写法:
       GridData gridData = new GridData(GridData.GRAB_HORIZONTAL);
 
   ②GridData.grabExcessVerticalSpace
       GridData gridData = new GridData();
       // 当容器大小变化时,组件所在行空间纵向随容器的变化而变化
       gridData.grabExcessVerticalSpace = true;
 
       等价写法:
       GridData gridData = new GridData(GridData.GRAB_VERTICAL);
 
3.GridData.FILL_HORIZONTAL、GridData.FILL_VERTICAL、GridData.FILL_BOTH
   ① GridData.FILL_HORIZONTAL:
        GridData gridData = new GridData();
        // 横向对齐方式
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
  
        等价写法:
        GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
 
    ②GridData.FILL_VERTICAL
        GridData gridData = new GridData();
        // 纵向对齐方式
        gridData.verticalAlignment = GridData.FILL;
        gridData.grabExcessVerticalSpace = true;
  
        等价写法:
        GridData gridData = new GridData(GridData.FILL_VERTICAL);
 
    ③GridData.FILL_BOTH
        GridData gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
  
        等价写法:
        GridData gridData = new GridData(GridData.FILL_BOTH);

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐