问题
My current Mainscreen looks like :
The current code for each line of display is like:
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);
But need to it be positioned in a straight line :
回答1:
There are many possibilities. You can adjust the margin of the container that holds list of services. Also you can make your custom field manager, and many more.
Option 1: Adjusting margin
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);
Option 2: Using a custom field Manger
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);
[Added Later]
As alishaik786 suggested in comment, if you use LabelField instead of RichTextField, you can check following code, which doesn't use any margin:
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);
回答2:
Better to use LabelField instead of RichTextField. If you want the result on only RichTextField the below code is not suit for you; Then try to use our "Rupak" code;
This below code uses LabelField instead of 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);
}
}
I got like this when field align center, right and left:
回答3:
Try this..
LabelField lbl=new LabelField("• Computer & Laptop\r\n• Modem / Router / Switches\r\n•
Printer / Scanner\r\n• Tablet",LabelField.FIELD_HCENTER);
来源:https://stackoverflow.com/questions/8810304/making-the-richtextfield-horizontally-centered-in-blackberry