0

我在HFM中添加了多個標籤字段,其文本來自String []。假設有5個標籤。我想要標籤在左側,HCentre和HFM右側。我創建labfetields像,在黑莓5.0水平字段管理器中對齊標籤文本

String[] labels = {------}; 

for(int i=0;labels.length;i++) 
{ 

     LabelField labelField = new LabelField(labels[i],Field.FOCUSABLE|Field.ACTION_INVOKE){ 

     public void paint(Graphics g) { 
     ------- 
     ------- 
     } 

     protected void layout(int width, int height){ 

      super.layout(Display.getWidth()/3, getContentHeight());       
      setExtent(Display.getWidth()/3, getContentHeight()); 
      //setPosition(Display.getWidth()/3,0); 
}; 

HFM.add(labelField); 

} 

但它不是以我想要的格式。

怎麼辦?我在Field的佈局()中做錯了什麼?如果我錯了,請糾正我。

enter image description here

我附上什麼,我現在得到的截圖,但我想對齊是第一個labelField屬性HFM,第二次在該中心和3日在HFM權的左派,但只有事情是我從String []獲取LabelTexts。

+1

我認爲GridFieldManager滿足您的需求:http://docs.blackberry.com/en/developers/deliverables/11958/Create_grid_layout_877557_11.jsp – rosco

+0

@Nate,我使用完全相同的上述代碼。我不是從LabelField類中調用HFM.add(labelField),而是在for()內調用,因爲我在運行時通過從String []中獲取文本來添加LabelField。 – Shreyas

+0

基本上,我想要的是對齊HFM的左,中,右標籤,我沒有收到。我不知道哪一個是第一,第二和第三個LabelField。 – Shreyas

回答

1
  According to your requirement , this Sample class will help you .. 

     import net.rim.device.api.ui.*; 

     public class JustifiedEvenlySpacedHorizontalFieldManager extends Manager 
     { 
      private static final int SYSTEM_STYLE_SHIFT = 32; 

      public JustifiedEvenlySpacedHorizontalFieldManager() 
      { 
       this(0); 
      } 

      public JustifiedEvenlySpacedHorizontalFieldManager(long style) 
      { 
       super(USE_ALL_WIDTH | style); 
      } 

      protected void sublayout(int width, int height) 
      { 
       int availableWidth = width; 

       int numFields = getFieldCount(); 
       int maxPreferredWidth = 0; 
       int maxHeight = 0; 


       // There may be a few remaining pixels after dividing up the space 
       // we must split up the space between the first and last buttons 
       int fieldWidth = width/numFields; 
       int firstFieldExtra = 0; 
       int lastFieldExtra = 0; 

       int unUsedWidth = width - fieldWidth * numFields; 
       if(unUsedWidth > 0) { 
        firstFieldExtra = unUsedWidth/2; 
        lastFieldExtra = unUsedWidth - firstFieldExtra; 
       } 

       int prevRightMargin = 0; 

       // Layout the child fields, and calculate the max height 
       for(int i = 0; i < numFields; i++) { 

        int nextLeftMargin = 0; 
        if(i < numFields - 1) { 
         Field nextField = getField(i); 
         nextLeftMargin = nextField.getMarginLeft(); 
        } 

        Field currentField = getField(i); 
        int leftMargin = i == 0 ? currentField.getMarginLeft() : Math.max(prevRightMargin, currentField.getMarginLeft())/2; 
        int rightMargin = i < numFields - 1 ? Math.max(nextLeftMargin, currentField.getMarginRight())/2 : currentField.getMarginRight(); 
        int currentVerticalMargins = currentField.getMarginTop() + currentField.getMarginBottom(); 
        int currentHorizontalMargins = leftMargin + rightMargin; 

        int widthForButton = fieldWidth; 
        if(i == 0) { 
         widthForButton = fieldWidth + firstFieldExtra; 
        } else if(i == numFields -1) { 
         widthForButton = fieldWidth + lastFieldExtra; 
        } 
        layoutChild(currentField, widthForButton - currentHorizontalMargins, height - currentVerticalMargins); 
        maxHeight = Math.max(maxHeight, currentField.getHeight() + currentVerticalMargins); 

        prevRightMargin = rightMargin; 
        nextLeftMargin = 0; 
       } 

       // Now position the fields, respecting the Vertical style bits 
       int usedWidth = 0; 
       int y; 
       prevRightMargin = 0; 
       for(int i = 0; i < numFields; i++) { 

        Field currentField = getField(i); 
        int marginTop = currentField.getMarginTop(); 
        int marginBottom = currentField.getMarginBottom(); 
        int marginLeft = Math.max(currentField.getMarginLeft(), prevRightMargin); 
        int marginRight = currentField.getMarginRight(); 

        switch((int)((currentField.getStyle() & FIELD_VALIGN_MASK) >> SYSTEM_STYLE_SHIFT)) { 
         case (int)(FIELD_BOTTOM >> SYSTEM_STYLE_SHIFT): 
          y = maxHeight - currentField.getHeight() - currentField.getMarginBottom(); 
          break; 
         case (int)(FIELD_VCENTER >> SYSTEM_STYLE_SHIFT): 
          y = marginTop + (maxHeight - marginTop - currentField.getHeight() - marginBottom) >> 1; 
          break; 
         default: 
          y = marginTop; 
        } 
        setPositionChild(currentField, usedWidth + marginLeft, y); 
        usedWidth += currentField.getWidth() + marginLeft; 
        prevRightMargin = marginRight; 
       } 
       setExtent(width, maxHeight); 
      } 

     } 

     please have a look on this link :- 
     [http://supportforums.blackberry.com/t5/Java-Development/Implement-advanced-buttons-fields-and-managers/ta-p/488276][1] 
     you will get all kinds of Custom Components 
     Cheers ..!!! 





    [1]: http://supportforums.blackberry.com/t5/Java-Development/Implement-advanced-buttons-fields-and-managers/ta-p/488276