在黑莓中的网格布局中将字段右对齐

时间:2012-04-04 12:15:37

标签: blackberry

我希望我的应用程序将两个字段对齐,一个在左边,另一个在屏幕的最左边。为此,我使用GridLayoutManager类。这是我的代码

GridFieldManager gridFieldManager = new GridFieldManager(2,2, GridFieldManager.PREFERRED_SIZE_WITH_MAXIMUM); 

gridFieldManager.add(new ButtonField("Button One"), Field.FIELD_HCENTER);  
gridFieldManager.add(new ButtonField("Button Two"), Field.FIELD_RIGHT);  

gridFieldManager.add(new ButtonField("HC", Field.FIELD_HCENTER)); 
gridFieldManager.add(new ButtonField("RT", Field.FIELD_RIGHT));

add(gridFieldManager);

而且,这是我在模拟器中的输出

enter image description here

有人可以帮助我将Button Two对准屏幕的最右侧吗?任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:3)

基本上,这都是对齐标记和网格列属性的问题。

GridFieldManager样式更改为Manager.USE_ALL_WIDTH并将列属性设置为GridFieldManager.AUTO_SIZE会使所有可用空间在两列之间进行分割。

gridFieldManager.setColumnProperty(0, GridFieldManager.AUTO_SIZE, 0);
gridFieldManager.setColumnProperty(1, GridFieldManager.AUTO_SIZE, 0);

以下代码段

GridFieldManager gridFieldManager = new GridFieldManager(2,2, Manager.USE_ALL_WIDTH); 
gridFieldManager.setColumnProperty(0, GridFieldManager.AUTO_SIZE, 0);
gridFieldManager.setColumnProperty(1, GridFieldManager.AUTO_SIZE, 0);

gridFieldManager.add(new ButtonField("Button One"), Field.FIELD_LEFT);  
gridFieldManager.add(new ButtonField("Button Two"), Field.FIELD_RIGHT);  
gridFieldManager.add(new ButtonField("HC"), Field.FIELD_LEFT); 
gridFieldManager.add(new ButtonField("RT"), Field.FIELD_RIGHT);

add(gridFieldManager);

产生

enter image description here


这个稍微修改过的代码段

GridFieldManager gridFieldManager = new GridFieldManager(1,2, Manager.USE_ALL_WIDTH); 
gridFieldManager.setColumnProperty(0, GridFieldManager.AUTO_SIZE, 0);
gridFieldManager.setColumnProperty(1, GridFieldManager.AUTO_SIZE, 0);

VerticalFieldManager vfmLeft = new VerticalFieldManager();
vfmLeft.add(new ButtonField("Button One", Field.FIELD_HCENTER));  
vfmLeft.add(new ButtonField("HC", Field.FIELD_HCENTER)); 
gridFieldManager.add(vfmLeft, Field.FIELD_LEFT);  

VerticalFieldManager vfmRight = new VerticalFieldManager();
vfmRight.add(new ButtonField("Button Two", Field.FIELD_HCENTER));  
vfmRight.add(new ButtonField("RT", Field.FIELD_HCENTER));
gridFieldManager.add(vfmRight, Field.FIELD_RIGHT);  

add(gridFieldManager); 

产生

enter image description here


最后,为了说明我之前所说的关于两列中最终划分的可用空间的内容,以下代码片段

GridFieldManager gridFieldManager = new GridFieldManager(1,2, Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT); 
gridFieldManager.setColumnProperty(0, GridFieldManager.AUTO_SIZE, 0);
gridFieldManager.setColumnProperty(1, GridFieldManager.AUTO_SIZE, 0);
gridFieldManager.setRowProperty(0, GridFieldManager.AUTO_SIZE, 0);

VerticalFieldManager vfmLeft = new VerticalFieldManager(Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT);
vfmLeft.setBackground(BackgroundFactory.createSolidBackground(Color.CYAN));
gridFieldManager.add(vfmLeft);  

VerticalFieldManager vfmRight = new VerticalFieldManager(Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT);
vfmRight.setBackground(BackgroundFactory.createSolidBackground(Color.GRAY));
gridFieldManager.add(vfmRight);  

add(gridFieldManager);    

生成两列相同大小的列。

enter image description here

答案 1 :(得分:2)

您似乎应该使用 JustifiedHorizo​​ntalFieldManager 而不是GridFieldManager。 JustifiedHorizo​​ntalFieldManager不是BlackBerry SDK中包含的标准管理器,而是RIM稍后发布的一组UI组件的成员,以帮助开发人员构建更丰富的UI界面。您必须从here下载代码,将其添加到您的proyect,然后包含以下行:

JustifiedHorizontalFieldManager justifiedManager = new JustifiedHorizontalFieldManager(buttonOne, buttonTwo, false, USE_ALL_WIDTH );

答案 2 :(得分:2)

请使用以下课程。

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.container.HorizontalFieldManager;

public class HFMLeftFieldRightField extends HorizontalFieldManager {

 private Field leftField;
 private Field rightField;

 private final static int TOP_MARGIN = 0;
 private final static int LEFT_MARGIN = 30;

 public HFMLeftFieldRightField() {
  super(USE_ALL_WIDTH);
 }

 public HFMLeftFieldRightField(boolean isQatari) {
  super(USE_ALL_WIDTH | Field.FIELD_LEFT);
 }

 public void sublayout(int maxWidth, int maxHeight) {

  super.sublayout(maxWidth, maxHeight);
  int width = getWidth();

  if (rightField != null) {
   int x = width - rightField.getWidth() - LEFT_MARGIN;
   int y = TOP_MARGIN;
   setPositionChild(rightField, x, y);
  }
  if (leftField != null) {
   int y = TOP_MARGIN+rightField.getHeight()/5;
   int x = LEFT_MARGIN;
   setPositionChild(leftField, 0, y);
  }

  setExtent(maxWidth, rightField.getHeight() + TOP_MARGIN * 2);
 }

 public void setLeftButton(Field leftField) {
  this.leftField = leftField;
super.add(leftField);
 }

 public void setRightButton(Field rightField) {
  this.rightField = rightField;
  super.add(rightField);
 }


}

以这种方式添加字段。

HFMLeftFieldRightField hfm = new HFMLeftFieldRightField();
hfm.setLeftButton(new EditField("Left"));
hfm.setRightButton(new EditField("Right"));
add(hfm);

更多细节http://keraisureshvblackberry.blogspot.in/2012/02/there-are-very-common-there-there-are.html

希望有帮助..

答案 3 :(得分:0)

将gridFieldManager构造函数的style参数更改为 GridFieldManager.USE_ALL_WIDTH

应该是这样的

GridFieldManager gridFieldManager = new GridFieldManager(2,2,GridFieldManager.USE_ALL_WIDTH );