如何使用水平场使标签在屏幕上居中

发布于 2024-12-24 02:38:14 字数 655 浏览 2 评论 0原文

我想将徽标和文本字段垂直和水平居中于屏幕中间。实现这一目标的最佳方法是什么?

到目前为止我一直在尝试这个..

public upd8rMainScreen(){

    Bitmap logoBitmap = Bitmap.getBitmapResource("upd8rLOGO.png");
    bitmapField = new BitmapField(logoBitmap, Field.FIELD_VCENTER);
    labelfield = new LabelField("Tap Your Phone Onto The Reader To Activate",Field.FIELD_VCENTER);


    topHfm = new HorizontalFieldManager(Manager.VERTICAL_SCROLL);
    middleHfm = new HorizontalFieldManager(Manager.VERTICAL_SCROLL);

    vfm = new VerticalFieldManager();
    topHfm.add(bitmapField);
    middleHfm.add(labelfield);

    vfm.add(topHfm);
    vfm.add(middleHfm);
    add(vfm);

}

I want to center a logo and a textfield in the middle of the screen, both vertically and horizontally. What is the best way to achieve this?

i have been trying with this so far..

public upd8rMainScreen(){

    Bitmap logoBitmap = Bitmap.getBitmapResource("upd8rLOGO.png");
    bitmapField = new BitmapField(logoBitmap, Field.FIELD_VCENTER);
    labelfield = new LabelField("Tap Your Phone Onto The Reader To Activate",Field.FIELD_VCENTER);


    topHfm = new HorizontalFieldManager(Manager.VERTICAL_SCROLL);
    middleHfm = new HorizontalFieldManager(Manager.VERTICAL_SCROLL);

    vfm = new VerticalFieldManager();
    topHfm.add(bitmapField);
    middleHfm.add(labelfield);

    vfm.add(topHfm);
    vfm.add(middleHfm);
    add(vfm);

}

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

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

发布评论

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

评论(2

﹏半生如梦愿梦如真 2024-12-31 02:38:14

也许它可以帮助您尝试此

Bitmap logoBitmap = Bitmap.getBitmapResource("basket.png");
        bitmapField = new BitmapField(logoBitmap,Field.FIELD_HCENTER);
        labelfield = new LabelField("Tap Your Phone Onto ",Field.FIELD_HCENTER);
//     
        VerticalFieldManager vrt=new VerticalFieldManager(USE_ALL_WIDTH)
        {
            protected void sublayout(int maxWidth, int maxHeight) {

                super.sublayout(Display.getWidth(),Display.getHeight());
                setExtent(Display.getWidth(),Display.getHeight());
            }
        };
        Font f=labelfield.getFont();
        int hight1=f.getAdvance(labelfield.getText());
        int k=labelfield.getPreferredHeight();

        int number=hight1/Display.getWidth()+1;
        int hight2=logoBitmap.getHeight();
        int padding=(Display.getHeight()-((number*k)+hight2))/2;
        if(padding>0){
            bitmapField.setPadding(padding,0,0,0);
        }

        vrt.add(bitmapField);
        vrt.add(labelfield);

图像显示,如下所示

在此处输入图像描述

May be it help you Try this

Bitmap logoBitmap = Bitmap.getBitmapResource("basket.png");
        bitmapField = new BitmapField(logoBitmap,Field.FIELD_HCENTER);
        labelfield = new LabelField("Tap Your Phone Onto ",Field.FIELD_HCENTER);
//     
        VerticalFieldManager vrt=new VerticalFieldManager(USE_ALL_WIDTH)
        {
            protected void sublayout(int maxWidth, int maxHeight) {

                super.sublayout(Display.getWidth(),Display.getHeight());
                setExtent(Display.getWidth(),Display.getHeight());
            }
        };
        Font f=labelfield.getFont();
        int hight1=f.getAdvance(labelfield.getText());
        int k=labelfield.getPreferredHeight();

        int number=hight1/Display.getWidth()+1;
        int hight2=logoBitmap.getHeight();
        int padding=(Display.getHeight()-((number*k)+hight2))/2;
        if(padding>0){
            bitmapField.setPadding(padding,0,0,0);
        }

        vrt.add(bitmapField);
        vrt.add(labelfield);

image display like following

enter image description here

☆獨立☆ 2024-12-31 02:38:14

您可以尝试像这样VerticalFieldManager重新设置边距

topHfm = new HorizontalFieldManager(Manager.FIELD_HCENTER | Manager.FIELD_VCENTER);
midHfm = new HorizontalFieldManager(Manager.FIELD_HCENTER | Manager.FIELD_VCENTER);

topHfm.add(bitmapField);
midHfm.add(new LabelField("My Label", Manager.FIELD_HCENTER));

VerticalFieldManager vfm = new VerticalFieldManager(Manager.FIELD_VCENTER);
vfm.add(topHfm);
vfm.add(midHfm);

int screenHeight = Display.getHeight();
int screenWidth = Display.getWidth();

int totalContentHeight = topHfm.getPreferredHeight()+ midHfm.getPreferredHeight();
int maxContentWidth = (topHfm.getPreferredWidth()>midHfm.getPreferredWidth()) ? topHfm.getPreferredWidth() : midHfm.getPreferredWidth() ;

int marginTop = (screenHeight>totalContentHeight)? (screenHeight - totalContentHeight) / 2 : 0;
int marginLeft = (screenWidth>maxContentWidth)? (screenWidth - maxContentWidth ) / 2 : 0;

vfm.setMargin(marginTop, 0, 0, marginLeft);

add(vfm);

我并不认为这是“最佳方法” 这样做,但我希望它适合您的情况。

You can try re-setting the margin of the VerticalFieldManager like this :

topHfm = new HorizontalFieldManager(Manager.FIELD_HCENTER | Manager.FIELD_VCENTER);
midHfm = new HorizontalFieldManager(Manager.FIELD_HCENTER | Manager.FIELD_VCENTER);

topHfm.add(bitmapField);
midHfm.add(new LabelField("My Label", Manager.FIELD_HCENTER));

VerticalFieldManager vfm = new VerticalFieldManager(Manager.FIELD_VCENTER);
vfm.add(topHfm);
vfm.add(midHfm);

int screenHeight = Display.getHeight();
int screenWidth = Display.getWidth();

int totalContentHeight = topHfm.getPreferredHeight()+ midHfm.getPreferredHeight();
int maxContentWidth = (topHfm.getPreferredWidth()>midHfm.getPreferredWidth()) ? topHfm.getPreferredWidth() : midHfm.getPreferredWidth() ;

int marginTop = (screenHeight>totalContentHeight)? (screenHeight - totalContentHeight) / 2 : 0;
int marginLeft = (screenWidth>maxContentWidth)? (screenWidth - maxContentWidth ) / 2 : 0;

vfm.setMargin(marginTop, 0, 0, marginLeft);

add(vfm);

I don't claim this is the "best way" to do this, but I hope it'll work for your case.

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