1

在此处输入图像描述我制作了一个水平场管理器,它将占据屏幕的全宽和 10% 的高度。现在我想分别向这个管理器添加 3 个字段。第一个是 BitmapField(屏幕宽度的 10%),第二个是 LabelField(屏幕宽度的 80%),第三个是 BitmapField(屏幕宽度的 10%)。

剩下的就是将这些字段的内容集中在字段本身的中心。

这是代码: -

public final class MyScreen extends MainScreen
{
public MyScreen()
{   
    final int disWidth = Display.getWidth();
    final int disHeight = Display.getHeight();
    final int heightHFM = (disHeight*10)/100;

    HorizontalFieldManager hfm = new HorizontalFieldManager(){
        protected void sublayout(int maxWidth, int maxHeight) {
            // TODO Auto-generated method stub
            super.sublayout(maxWidth, maxHeight);
            this.setExtent(disWidth,heightHFM);

    }
    };
    hfm.setBackground(BackgroundFactory.createSolidBackground(Color.GRAY));

    BitmapField bmp_leftArrow = new BitmapField(Bitmap.getBitmapResource("arrow_left.png")){
        protected void layout(int width, int height) {
            // TODO Auto-generated method stub
            super.layout(width, height);
            this.setExtent((disWidth*10)/100, height);
        }
    };
    bmp_leftArrow.setBackground(BackgroundFactory.createSolidBackground(Color.BLUE));

    LabelField lbl_tit = new LabelField("Current Date"){
        protected void layout(int width, int height) {
            // TODO Auto-generated method stub
            super.layout(width, height);
            this.setExtent((disWidth*80)/100, height);
        }
    };
    lbl_tit.setBackground(BackgroundFactory.createSolidBackground(Color.RED));
    BitmapField bmp_rightArrow = new BitmapField(Bitmap.getBitmapResource("arrow_left.png")){
        protected void layout(int width, int height) {
            // TODO Auto-generated method stub
            super.layout(width, height);
            this.setExtent((disWidth*10)/100, height);
        }
    };
    bmp_rightArrow.setBackground(BackgroundFactory.createSolidBackground(Color.GREEN));
    hfm.add(bmp_leftArrow);
    hfm.add(lbl_tit);
    hfm.add(bmp_rightArrow);
    add(hfm);

}
}

当我们在android中使用任何视图的重力时需要实现相同的输出

4

2 回答 2

3

For custom positioning and alignment of the child fields, A dedicated FieldManger with specified layout and field positioning is useful. You can try the following CustomHorizontalFieldManager which extends Manager.

Generated Output

enter image description here

Using the Manager

public class MyScreen extends MainScreen
{
    public MyScreen()
    {        
        CustomHorizontalFieldManager chfm = new CustomHorizontalFieldManager();
        Background bg = BackgroundFactory.createSolidBackground(Color.BROWN);
        chfm.setBackground(bg);

        BitmapField bmf0 = new BitmapField(Bitmap.getBitmapResource("img0.png"));
        BitmapField bmf1 = new BitmapField(Bitmap.getBitmapResource("img1.png"));

        CutomLabelField lblf = new CutomLabelField("Current Date");

        bg = BackgroundFactory.createSolidBackground(Color.GREEN);
        lblf.setBackground(bg);

        bg = BackgroundFactory.createSolidBackground(Color.GREEN);
        bmf0.setBackground(bg);

        bg = BackgroundFactory.createSolidBackground(Color.GREEN);
        bmf1.setBackground(bg);

        chfm.add(bmf0);
        chfm.add(lblf);
        chfm.add(bmf1);

        add(chfm);
    }
}

Implementation of the Manager

class CustomHorizontalFieldManager extends Manager {

    public CustomHorizontalFieldManager() {
        super(0);
    }

    private int width0, width1, width2;
    private Field f0, f1, f2;
    int x0, x1, x2, y0, y1, y2;

    protected void sublayout(int width, int height) {
        if (getFieldCount() == 3) {
            // calculate total width, total height
            width = Display.getWidth();
            height = getPercentage(Display.getHeight(), 10);

            // calculate field's width
            width0 = getPercentage(width, 10);
            width1 = getPercentage(width, 80);
            width2 = getPercentage(width, 10);

            f0 = getField(0);
            f1 = getField(1);
            f2 = getField(2);

            // layout all the child
            layoutChild(f0, width0, height);
            layoutChild(f1, width1, height);
            layoutChild(f2, width2, height);

            // Specify position of the child.           
            // Alignment of the fields can be adjusted here.
            // Following lines will align them
            // on center, both vertically and horizontally.
            x0 = (width0 - f0.getWidth()) / 2;
            x1 = width0 + (width1 - f1.getWidth()) / 2;
            x2 = width0 + width1 + (width2 - f2.getWidth()) / 2;

            y0 = (height - f0.getHeight()) / 2;
            y1 = (height - f1.getHeight()) / 2;
            y2 = (height - f2.getHeight()) / 2;

            setPositionChild(f0, x0, y0);
            setPositionChild(f1, x1, y1);
            setPositionChild(f2, x2, y2);

            setExtent(width, height);
        } else {
            // The manager contains some
            // invalid number of fields.

            // Make manager invisible.
            setExtent(0, 0);
        }
    }   

    int getPercentage(int value, int percent) {
        return (value * percent) / 100;
    }
}

Draft implementation of CustomLabelField

class CutomLabelField extends Field {
    private String text;

    public CutomLabelField(String text) {
        this.text = (text == null) ? "" : text;
    }

    int xText;
    int yText;  
    int availableWidth;

    protected void layout(int width, int height) {      
        // positioning text.
        int textWidth = getFont().getAdvance(text);
        availableWidth = width - getPaddingLeft() - getPaddingRight();
        if (availableWidth < textWidth) {
            xText = getPaddingLeft();
        } else {
            xText = getPaddingLeft() + (availableWidth - textWidth) / 2;
        }
        yText = (height - getFont().getHeight()) / 2;

        // using all width and height
        setExtent(width, height);
    }

    protected void paint(Graphics graphics) {
        // set text color
        graphics.setColor(Color.BLACK);
        graphics.drawText(text, xText, yText, DrawStyle.ELLIPSIS, availableWidth);
    }

    public void setText(String text) {
        this.text = (text == null) ? "" : text;
        updateLayout();
    }

    public String getText() {
        return this.text;
    }
}
于 2012-05-17T04:57:21.763 回答
0

而不是将您的LabelField(variable lbl_tit) 直接添加到您的HorizontalFieldManager添加到VerticalFieldManager第一个。然后添加VerticalFieldManager到您的HorizontalFieldManager.

当您创建LabelField设置Field.FIELD_HCENTER样式位时,将其居中对齐在VerticalFieldManager.

这是我的代码:

public final class MyScreen extends MainScreen
{
    public MyScreen()
    {   
        final int disWidth = Display.getWidth();
        final int disHeight = Display.getHeight();
        final int heightHFM = (disHeight*10)/100;

        HorizontalFieldManager hfm = new HorizontalFieldManager(){
            protected void sublayout(int maxWidth, int maxHeight) {
                super.sublayout(maxWidth, maxHeight);
                this.setExtent(disWidth,heightHFM);

            }
        };
        hfm.setBackground(BackgroundFactory.createSolidBackground(Color.GRAY));

        BitmapField bmp_leftArrow = new BitmapField(Bitmap.getBitmapResource("arrow_left.png")){
            protected void layout(int width, int height) {
                super.layout(width, height);
                this.setExtent((disWidth*10)/100, height);
            }
        };
        bmp_leftArrow.setBackground(BackgroundFactory.createSolidBackground(Color.BLUE));

        VerticalFieldManager vfm = new VerticalFieldManager(USE_ALL_WIDTH){

            protected void sublayout(int width, int height) {
                super.sublayout((disWidth*80)/100, height);
                this.setExtent((disWidth*80)/100, height);                   
            }        
        };

        vfm.setBackground(BackgroundFactory.createSolidBackground(Color.RED));

        LabelField lbl_tit = new LabelField("Current Date", Field.FIELD_HCENTER);
        vfm.add(lbl_tit);

        BitmapField bmp_rightArrow = new BitmapField(Bitmap.getBitmapResource("arrow_left.png")){
            protected void layout(int width, int height) {
                super.layout(width, height);
                this.setExtent((disWidth*10)/100, height);
            }
        };
        bmp_rightArrow.setBackground(BackgroundFactory.createSolidBackground(Color.GREEN));
        hfm.add(bmp_leftArrow);
        hfm.add(vfm);
        hfm.add(bmp_rightArrow);
        add(hfm);

    }
}

从您的 LabelField 中删除覆盖的layout方法,您不再需要它。它应该如下所示:

模拟器截图

于 2012-05-16T09:14:59.830 回答