黑莓文字换行

发布于 2024-09-30 18:13:19 字数 5594 浏览 1 评论 0原文

我无法在 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);
    }
}

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);
                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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

古镇旧梦 2024-10-07 18:13:19

问题出在routeManager2 中的布局逻辑。

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

LabelField 或 RichTextField 的首选宽度将是当前字体大小中与其关联的字符串的宽度。如果可能的话,它更愿意在一行上显示所有内容。因此,不要传递其首选项,而是将提供的宽度传递给 sublayout(int, int)。

layoutChild(bloodBankName, width, getPreferredHeight());

希望这有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文