0

我想在屏幕底部添加一个带有图像和字符串的栏,我希望间距相同

img1 string img2|img3 ,这就是底栏的样子,下面的代码没有正常工作,我正在对齐,最后一个 iamge 正在消失。

HorizontalFieldManager horizontalFieldManager = new HorizontalFieldManager(
                FIELD_BOTTOM) {
            public void paint(Graphics graphics) {
                graphics.setBackgroundColor(0x316AC5);
                graphics.clear();
                super.paint(graphics);
            }
        };
        Bitmap fadeBitmap = Bitmap
                .getBitmapResource("GE_TimeZone_Fade_blue.PNG");
        Bitmap clockBitmap = Bitmap.getBitmapResource("GE_Cal_icon_blue.PNG");
        Bitmap tzBitmap = Bitmap
                .getBitmapResource("GE_TimeZone_Button_blue.PNG");

        final ImageButtonField unfocus = new ImageButtonField("",
                Field.FOCUSABLE | FIELD_LEFT, "GE_TimeZone_Fade_blue.PNG",
                "GE_TimeZone_Fade_blue.PNG", 0xFFFFFF);
        LabelField test = new LabelField("hello");
        final ImageButtonField bitmapField = new ImageButtonField("",
                Field.FOCUSABLE | FIELD_HCENTER, "GE_Cal_icon_blue.PNG",
                "GE_Cal_icon_onSelect.PNG", 0xFFFFFF);

    final ImageButtonField bitmapField1 = new ImageButtonField("",
            Field.FOCUSABLE | FIELD_RIGHT, "GE_TimeZone_Button_blue.PNG",
            "GE_TimeZone_Btn_OnSelect.PNG", 0xFFFFFF);

    int margin = ((Display.getWidth() - (fadeBitmap.getWidth()
            + clockBitmap.getWidth() + tzBitmap.getWidth() + test
            .getWidth())) / 4);

    unfocus.setMargin(0, margin, 0, 0);
    test.setMargin(0, margin, 0, 0);
    bitmapField.setMargin(0, margin, 0, 0);
    bitmapField1.setMargin(0, margin, 0, 0);

    horizontalFieldManager.add(unfocus);
    horizontalFieldManager.add(test);
    horizontalFieldManager.add(bitmapField);
    horizontalFieldManager.add(bitmapField1);

    this.setStatus(horizontalFieldManager);
4

2 回答 2

1

您使用的代码有问题。检查以下行。

int margin = ((Display.getWidth() - (fadeBitmap.getWidth()
            + clockBitmap.getWidth() + tzBitmap.getWidth() + test
            .getWidth())) / 4);

如果调用其父管理器的布局方法,则任何FieldgetWidth()getHeight()都将返回有效值。因此,使用,调整边距是不安全的。getWidth()getHeight()

但是可以Field通过扩展Horizo​​ntalFieldManager来控制 s 的对齐和位置。检查以下代码和输出以了解如何完成。

输出

在此处输入图像描述

使用 StatusFieldManager:

StatusFieldManager statusFieldManager = new StatusFieldManager();
statusFieldManager.setBackground(BackgroundFactory.createSolidBackground(0x316AC5));

final Bitmap bmTest = Bitmap.getBitmapResource("bitmap.png");

BitmapField bmOne = new BitmapField(bmTest, Field.FOCUSABLE | FIELD_LEFT);
BitmapField bmTwo = new BitmapField(bmTest, Field.FOCUSABLE | FIELD_LEFT);
BitmapField bmThree = new BitmapField(bmTest, Field.FOCUSABLE | FIELD_LEFT);
LabelField lblTest = new LabelField("Test");

statusFieldManager.add(bmOne);
statusFieldManager.add(lblTest);
statusFieldManager.add(bmTwo);
statusFieldManager.add(bmThree);

setStatus(statusFieldManager);

StatusFieldManager 的实现

class StatusFieldManager extends HorizontalFieldManager {
    protected void sublayout(int width, int height) {
        int numField = getFieldCount();
        Field f;
        int nHeight = 0, maxFieldWidth = width / 4;
        if (numField == 4) {
            f = getField(0);
            layoutChild(f, maxFieldWidth, height);
            nHeight = Math.max(nHeight, f.getHeight());

            f = getField(1);
            layoutChild(f, maxFieldWidth, height);
            nHeight = Math.max(nHeight, f.getHeight());

            f = getField(2);
            layoutChild(f, maxFieldWidth, height);
            nHeight = Math.max(nHeight, f.getHeight());

            f = getField(3);
            layoutChild(f, maxFieldWidth, height);
            nHeight = Math.max(nHeight, f.getHeight());

            // set position of the child fields
            int x = 0, y = 0;
            int requiredFieldWidth = 0;
            for (int i=0;i<numField;i++) {
                requiredFieldWidth += getField(i).getWidth(); 
            }
            int spaceBetweenFields = (width - requiredFieldWidth) / (numField - 1);

            for (int i=0;i<numField;i++) {
                setPositionChild(getField(i), x, (nHeight - getField(i).getHeight()) / 2);
                x += getField(i).getWidth() + spaceBetweenFields;
            }
            setExtent(width, nHeight);

        } else {
            setExtent(0, 0);
        }
    }
}
于 2012-06-07T10:53:36.573 回答
0

试试这个 -

unfocus.setMargin(0, margin, 0, 0);
test.setMargin(0, 10, 0, 0);
bitmapField.setMargin(0, 10, 0, 0);
bitmapField1.setMargin(0, 10, 0, 0);
于 2012-06-07T10:36:23.230 回答