我无法在BlackBerry屏幕上对齐文字。我尝试过使用LabelField或RichTextField,但文本没有按照我想要的方式对齐。它水平对齐并隐藏在屏幕中。我希望文本在下一行包裹,当它水平到达屏幕的末尾,而不是隐藏。 这是我的代码 -
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
public class DetailBloodBank extends MainScreen
{
String resultData = "", location = "", phoneNumber = "";
LabelField bloodBankName, locationLabel, phoneNumLabel;
String bloodBank = "";
LabelField locationDetail, phoneNumDetail;
public DetailBloodBank(String data) {
super(NO_VERTICAL_SCROLL);
int height = Display.getHeight();
int widhth = Display.getWidth();
//SizedVFM horizontalFieldManager = new SizedVFM(widhth,height);
HorizontalFieldManager horizontalFieldManager = new HorizontalFieldManager(HorizontalFieldManager.NO_VERTICAL_SCROLL)
{
protected void paint(Graphics g) {
g.setBackgroundColor(Color.BLACK);
g.clear();
super.paint(g);
}
};
//setBanner(horizontalFieldManager);
resultData = data;
System.out.println(resultData);
bloodBankName = new LabelField(resultData)
{
public void paint(Graphics g) {
g.setColor(Color.WHITE);
super.paint(g);
}
};
Font font = Font.getDefault().derive(Font.BOLD, 8, Ui.UNITS_pt);
bloodBankName.setFont(font);
locationLabel = new LabelField("Location")
{
public void paint(Graphics g) {
g.setColor(Color.WHITE);
super.paint(g);
}
};
Font font2 = Font.getDefault().derive(Font.BOLD, 8, Ui.UNITS_pt);
locationLabel.setFont(font2);
phoneNumLabel = new LabelField("Phone Number")
{
public void paint(Graphics g) {
g.setColor(Color.WHITE);
super.paint(g);
}
};
Font font3 = Font.getDefault().derive(Font.BOLD, 8, Ui.UNITS_pt);
phoneNumLabel.setFont(font3);
DBHelpers dbh = new DBHelpers();
dbh.retrieveDetails(resultData);
location = dbh.getLocation();
phoneNumber = dbh.getPhoneNumber();
dbh.getConnectionClose();
phoneNumDetail = new LabelField(phoneNumber)
{
public void paint(Graphics g) {
g.setColor(Color.WHITE);
super.paint(g);
}
};
Font font4 = Font.getDefault().derive(Font.PLAIN, 6, Ui.UNITS_pt);
phoneNumDetail.setFont(font4);
locationDetail = new LabelField(location)
{
public void paint(Graphics g) {
g.setColor(Color.WHITE);
super.paint(g);
}
};
Font font5 = Font.getDefault().derive(Font.PLAIN, 6, Ui.UNITS_pt);
locationDetail.setFont(font5);
final SeparatorField sepfield2 = new SeparatorField(SeparatorField.LINE_HORIZONTAL | Field.USE_ALL_WIDTH)
{
protected void paint(Graphics g) {
g.setBackgroundColor(Color.WHITE);
g.clear();
super.paint(g);
}
protected void layout(int maxWidth, int maxHeight) {
int width = Display.getWidth();
int height = 25; //height of the manager
super.layout(1250, 15);
}
};
final VerticalFieldManager verticalFieldManager = new VerticalFieldManager(NO_HORIZONTAL_SCROLL);
verticalFieldManager.add(locationLabel);
verticalFieldManager.add(locationDetail);
final VerticalFieldManager verticalFieldManager2 = new VerticalFieldManager(NO_HORIZONTAL_SCROLL);
verticalFieldManager2.add(phoneNumLabel);
verticalFieldManager2.add(phoneNumDetail);
VerticalFieldManager routeManager2 = new VerticalFieldManager()
{
protected void sublayout(int width, int height) {
layoutChild(bloodBankName, getPreferredWidth(), getPreferredHeight());
layoutChild(sepfield2, getPreferredWidth(), getPreferredHeight());
layoutChild(verticalFieldManager, getPreferredWidth(), getPreferredHeight());
layoutChild(verticalFieldManager2, getPreferredWidth(), getPreferredHeight());
setPositionChild(bloodBankName, 5, 15);
setPositionChild(sepfield2, 0, 35);
setPositionChild(verticalFieldManager, 0, 72);
int y = verticalFieldManager.getHeight();
System.out.println(y);
setPositionChild(verticalFieldManager2, 0, y + 90);
super.setExtent(width, height);
}
};
routeManager2.add(bloodBankName);
routeManager2.add(sepfield2);
routeManager2.add(verticalFieldManager);
routeManager2.add(verticalFieldManager2);
horizontalFieldManager.add(routeManager2);
add(horizontalFieldManager);
}
}
答案 0 :(得分:2)
问题在于routeManager2中的布局逻辑。
layoutChild(bloodBankName, getPreferredWidth(), getPreferredHeight());
LabelField或RichTextField的首选宽度将是当前字体大小与其关联的String的宽度。如果可能的话,它更愿意在一行上显示所有内容。因此,不是传递它的首选项,而是将提供的宽度传递给sublayout(int,int)。
layoutChild(bloodBankName, width, getPreferredHeight());
希望这有帮助。