2012-01-10 76 views
1

我現在Mainscreen樣子:使得RichTextField水平居中在黑莓

enter image description here

顯示的每一行目前的代碼是這樣的:

RichTextField WeFix1 = new RichTextField("• Computer & Laptop", RichTextField.TEXT_JUSTIFY_HCENTER); 

    VFMTitle1 = new VerticalFieldManager(Field.USE_ALL_WIDTH| Field.NON_FOCUSABLE); 

    HFMTitle1 = new HorizontalFieldManager(FIELD_HCENTER); 

    HFMTitle1.add(WeFix1); 

    VFMTitle1.add(HFMTitle1); 
add(VFMTitle21); 

但需要將其定位在直線:

enter image description here

+0

手段你要對每行作爲背景灰色的顏色?或者你需要直線最右邊和最左邊的位置? – 2012-01-11 04:27:13

+0

嘗試使用標籤字段,而不是RichTextField – alishaik786 2012-01-11 04:55:04

+0

由於RichTextField佔據了所有寬度,並且不會在其旁邊添加任何字段添加項。這時你必須使用sublayout()方法來提供一些寬度和高度;並給一些填充放置在屏幕中間。這就是爲什麼使用LabelField更好的原因 – alishaik786 2012-01-11 05:31:24

回答

2

有很多可能性。您可以調整容納服務列表的容器邊距。你也可以讓你的自定義現場經理,以及更多。

選項1:調整量

VerticalFieldManager vfmServices = new VerticalFieldManager(VerticalFieldManager.USE_ALL_WIDTH); 

vfmServices.add(new RichTextField("• Computer & Laptop")); 
vfmServices.add(new RichTextField("• Modem/Router/Switches")); 
vfmServices.add(new RichTextField("• Printer/Scanner")); 
vfmServices.add(new RichTextField("• Tablet")); 

final int horizontalMargin = 30; 
vfmServices.setMargin(0, horizontalMargin, 0, horizontalMargin); 

add(vfmServices); 

選項2:使用自定義字段經理

VerticalFieldManager vfmServiceLists = new VerticalFieldManager(VerticalFieldManager.USE_ALL_WIDTH); 

vfmServiceLists.add(new RichTextField("• Computer & Laptop")); 
vfmServiceLists.add(new RichTextField("• Modem/Router/Switches")); 
vfmServiceLists.add(new RichTextField("• Printer/Scanner")); 
vfmServiceLists.add(new RichTextField("• Tablet")); 

Manager mListContainer = new Manager(Manager.USE_ALL_WIDTH) { 

    final int horizontalMargin = 30; 

    protected void sublayout(int width, int height) { 
     // this manager designed to contain only one list container. 
     if (getFieldCount() == 1) { 
      Field child = getField(0); 
      layoutChild(child, width - 2 * horizontalMargin, height); 
      // adjust manager height. 
      height = child.getHeight(); 
      setPositionChild(child, horizontalMargin, 0); 
     } 

     setExtent(width, height);  
    }   
}; 

mListContainer.add(vfmServiceLists); 
add(mListContainer); 

[後來添加]

由於alishaik786建議的評論,如果你使用的LabelField代替的RichTextField,您可以查看以下代碼,該代碼不使用任何保證金:

VerticalFieldManager vfmServiceLists = new VerticalFieldManager(); 

vfmServiceLists.add(new LabelField("• Computer & Laptop")); 
vfmServiceLists.add(new LabelField("• Modem/Router/Switches")); 
vfmServiceLists.add(new LabelField("• Printer/Scanner")); 
vfmServiceLists.add(new LabelField("• Tablet")); 

Manager mListContainer = new Manager(Manager.USE_ALL_WIDTH) { 
    protected void sublayout(int width, int height) { 
     // this manager designed to contain only one list container. 
     if (getFieldCount() == 1) { 
      Field child = getField(0); 
      layoutChild(child, width, height); 
      // adjust manager height. 
      height = child.getHeight(); 
      setPositionChild(child, (width - child.getWidth())/2, 0); 
     } 

     setExtent(width, height);  
    }   
}; 

mListContainer.add(vfmServiceLists); 
add(mListContainer); 
+0

此代碼不適合所有設備,因爲我們必須更改每個設備的horizo​​ntalMargin。嘗試使用Label字段替代RichTextField。那麼你必須得到解決方案。如果你想在RichTextField中只使用Rupak代碼; – alishaik786 2012-01-11 04:56:32

+0

是的,這段代碼可能不適用於所有設備,我沒有寫這個解決通用問題。但根據顯示寬度調整邊距,高度並不難。 – Rupak 2012-01-11 05:04:57

+0

Rupak和@ alishaik786感謝您的解決方案...... Rupak的最後一段代碼沒有任何餘量,並且RichTextField似乎是一個完美的解決方案! – 2012-01-11 09:07:54

1

更好地使用LabelField而不是RichTextField。 如果你只想在RichTextField上得到結果,下面的代碼不適合你;然後嘗試使用我們的「Rupak」代碼;

這下面的代碼使用的LabelField代替RichTextField;

public class Abc extends MainScreen 
{ 
VerticalFieldManager vertical; 
Font font=Font.getDefault().derive(Font.BOLD, 18),small_font=Font.getDefault().derive(Font.PLAIN, 15); 
LabelField labelField,labelFieldTwo; 
public Abc() 
{ 
    createGUI(); 
} 

private void createGUI() 
{ 
    vertical=new VerticalFieldManager(USE_ALL_WIDTH); 
    vertical.setBackground(BackgroundFactory.createSolidBackground(Color.GREEN)); 
    HorizontalFieldManager hor=new HorizontalFieldManager(Field.FIELD_HCENTER);  

    hor.add(new LabelField(" We Fix ", Field.NON_FOCUSABLE)); 
    hor.setFont(font); 
    hor.setPadding(5, 0, 5, 0); 
    vertical.add(hor); 

    HorizontalFieldManager hr=new HorizontalFieldManager(Field.FIELD_HCENTER); 

    hr.add(new LabelField("."));   
    hr.add(new LabelField("Computer Science & Laptop", Field.NON_FOCUSABLE)); 
    hr.setFont(small_font); 
    vertical.add(hr); 

    HorizontalFieldManager hr1=new HorizontalFieldManager(Field.FIELD_HCENTER); 

    hr1.add(new LabelField("."));  
    hr1.add(new LabelField("Modem/Router/Switches", Field.NON_FOCUSABLE)); 
    hr1.setFont(small_font); 
    vertical.add(hr1); 

    //------------------- Up to now you want like this; and you can do like this type also like left align and right align; 
    HorizontalFieldManager hr2=new HorizontalFieldManager(Field.FIELD_LEFT); 

    hr2.add(new LabelField("."));  
    hr2.add(new LabelField("Printer/Scanner", Field.NON_FOCUSABLE)); 
    hr2.setFont(small_font); 
    vertical.add(hr2); 

    HorizontalFieldManager hr3=new HorizontalFieldManager(Field.FIELD_LEFT); 

    hr3.add(new LabelField("."));  
    hr3.add(new LabelField("Tablet", Field.NON_FOCUSABLE)); 
    hr3.setFont(small_font); 
    vertical.add(hr3); 

    HorizontalFieldManager hr4=new HorizontalFieldManager(Field.FIELD_RIGHT); 

    hr4.add(new LabelField("."));  
    hr4.add(new LabelField("Printer/Scanner", Field.NON_FOCUSABLE)); 
    hr4.setFont(small_font); 
    vertical.add(hr4); 

    HorizontalFieldManager hr5=new HorizontalFieldManager(Field.FIELD_RIGHT); 

    hr5.add(new LabelField("."));  
    hr5.add(new LabelField("Tablet", Field.NON_FOCUSABLE)); 
    hr5.setFont(small_font); 
    vertical.add(hr5); 

    vertical.setPadding(0, 0, 10, 0); 
    add(vertical); 
} 
} 

我喜歡這個領域時對準中心,左,右

enter image description here

+0

我認爲你是黑莓新手。澄清你和我們的疑惑來到這個聊天室:http://chat.stackoverflow.com/rooms/4014/knowledge-sharing-center-for-blackberry-and-java – alishaik786 2012-01-11 05:08:02

+0

LabelField沒有幫助使字段對齊在圖像二中的一條直線上.. – 2012-01-11 08:51:02

+0

OHHH .......!到目前爲止,我沒有看到第二張圖片; – alishaik786 2012-01-12 13:11:35

0

試試這個..

LabelField lbl=new LabelField("• Computer & Laptop\r\n• Modem/Router/Switches\r\n• 
Printer/Scanner\r\n• Tablet",LabelField.FIELD_HCENTER);