开发者

BlackBerry text wraping

I can't align text on the BlackBerry screen. I've tried using a LabelField or a RichTextField but the text is not getting aligned the way I want. It is aligned horizontally and hidden in the screen. I want the text to wrap on the next line, when it hits the end of the screen horizontally, rather than getting hidden. Here is my code -

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);
   开发者_Python百科             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);
    }
}


The problem is with your layout logic in routeManager2.

layoutChild(bloodBankName, getPreferredWidth(), getPreferredHeight());

The preferred width of the LabelField or RichTextField is going to be the width of the String associated with it in the current font size. It would prefer to show everything on one line if possible. So, instead of passing its preference, pass the width provided to sublayout(int, int).

layoutChild(bloodBankName, width, getPreferredHeight());

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜