如何使用黑莓中的水平字段管理器对齐labelfield

时间:2012-04-17 06:02:28

标签: blackberry

我想在Horizo​​ntalFieldManager中添加两个labelfield,我想显示两个标签,一个左对齐,另一个右对齐,这样

enter image description here

hfm1 = new HorizontalFieldManager(Manager.USE_ALL_WIDTH);
        hfm1.setMargin(10,0,10,0);
        l1 = new LabelField("Weight:");
        l2 = new LabelField("20");
        l1.setMargin(0,10,0,0);
        l2.setMargin(0,5,0,0);
        hfm1.add(l1);
        hfm1.add(l2);

3 个答案:

答案 0 :(得分:6)

试试此代码

LabelField labl1=new LabelField("weight");
LabelField labl2=new LabelField("20");
JustifiedHorizontalFieldManager JustifiedHorizontalFieldManager=new JustifiedHorizontalFieldManager(labl1,labl2,true);
add(JustifiedHorizontalFieldManager);

JustifiedHorizo​​ntalFieldManager类在下面给出 -

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Manager;

public class JustifiedHorizontalFieldManager extends Manager
{
    private static final int SYSTEM_STYLE_SHIFT = 32;

    public Field _leftField;
    public Field _rightField;

    private boolean _giveLeftFieldPriority;

    public JustifiedHorizontalFieldManager( Field leftField, Field rightField, boolean giveLeftFieldPriority )
    {
        this( leftField, rightField, giveLeftFieldPriority, Field.USE_ALL_WIDTH );
    }

    public JustifiedHorizontalFieldManager( Field leftField, Field rightField, boolean giveLeftFieldPriority, long style )
    {
        super( style );

        _leftField = leftField;
        _rightField = rightField;

        add( _leftField );
        add( _rightField );

        _giveLeftFieldPriority = giveLeftFieldPriority;
    }

    public JustifiedHorizontalFieldManager( boolean giveLeftFieldPriority, long style )
    {
        super( style );
        _giveLeftFieldPriority = giveLeftFieldPriority;
    }

    public void addLeftField( Field field )
    {
        if( _leftField != null ) {
            throw new IllegalStateException();
        }
        _leftField = field;
        add( _leftField );
    }

    public void addRightField( Field field )
    {
        if( _rightField != null ) {
            throw new IllegalStateException();
        }
        _rightField = field;
        add( _rightField );
    }

    public int getPreferredWidth()
    {
        return _leftField.getPreferredWidth() + _rightField.getPreferredWidth();
    }

    public int getPreferredHeight()
    {
        return Math.max( _leftField.getPreferredHeight(), _rightField.getPreferredHeight() );
    }

    protected void sublayout( int width, int height )
    {
        Field firstField;
        Field secondField;
        if( _giveLeftFieldPriority ) {
            firstField = _leftField;
            secondField = _rightField;
        } else {
            firstField = _rightField;
            secondField = _leftField;
        }

        int maxHeight = 0;

        int availableWidth = width;
        availableWidth -= _leftField.getMarginLeft();
        availableWidth -= Math.max( _leftField.getMarginRight(), _rightField.getMarginLeft() );
        availableWidth -= _rightField.getMarginRight();

        layoutChild( firstField, availableWidth, height - firstField.getMarginTop() - firstField.getMarginBottom() );
        maxHeight = Math.max( maxHeight, firstField.getMarginTop() + firstField.getHeight() + firstField.getMarginBottom() );
        availableWidth -= firstField.getWidth();

        layoutChild( secondField, availableWidth, height - secondField.getMarginTop() - secondField.getMarginBottom() );
        maxHeight = Math.max( maxHeight, secondField.getMarginTop() + secondField.getHeight() + secondField.getMarginBottom() );
        availableWidth -= secondField.getWidth();

        if( !isStyle( Field.USE_ALL_HEIGHT ) ) {
            height = maxHeight;
        }
        if( !isStyle( Field.USE_ALL_WIDTH ) ) {
            width -= availableWidth;
        }

        setPositionChild( _leftField, _leftField.getMarginLeft(), getFieldY( _leftField, height ) );
        setPositionChild( _rightField, width - _rightField.getWidth() - _rightField.getMarginRight(), getFieldY( _rightField, height ) );

        setExtent( width, height );
    }

    private int getFieldY( Field field, int height )
    {
        switch( (int)( ( field.getStyle() & FIELD_VALIGN_MASK ) >> SYSTEM_STYLE_SHIFT ) ) {
            case (int)( FIELD_BOTTOM >> SYSTEM_STYLE_SHIFT ):
                return height - field.getHeight() - field.getMarginBottom();
            case (int)( FIELD_VCENTER >> SYSTEM_STYLE_SHIFT ):
                return field.getMarginTop() + ( height - field.getMarginTop() - field.getHeight() - field.getMarginBottom() ) / 2;
            default:
                return field.getMarginTop();
        }
    }


    public Field getLeftField()
    {
        return _leftField;
    }

    public Field getRightField()
    {
        return _rightField;
    }

    public void replace( Field oldField, Field newField )
    {
        if( oldField == newField ) {
            // Nothing to do
            return;
        }

        if( oldField == _leftField ) {
            _leftField = newField;
        } else if( oldField == _rightField ) {
            _rightField = newField;
        }
        add( newField );
        delete( oldField );
    }

}

答案 1 :(得分:5)

恕我直言,最优雅的解决方案是

HorizontalFieldManager hfm = new HorizontalFieldManager(Manager.USE_ALL_WIDTH);
hfm.setMargin(10,0,10,0);

LabelField l1 = new LabelField("Weight:");
l1.setMargin(0,10,0,0);
hfm.add(l1);

VerticalFieldManager vfm = new VerticalFieldManager(Manager.USE_ALL_WIDTH);
LabelField l2 = new LabelField("20", Field.FIELD_RIGHT);
l2.setMargin(0,5,0,0);
vfm.add(l2);
hfm.add(vfm);

add(hfm);

和结果

enter image description here

答案 2 :(得分:0)

另一种方法是,您可以使用两个标签制作自定义labelfield,并将其添加到您想要的位置。像

一样
public class LableFieldWithTwoLable extends LabelField {

private final String lbl2;
private final String lbl1;

public LableFieldWithTwoLable(String lbl1,String lbl2) {
    super();
    this.lbl1 = lbl1;
    this.lbl2 = lbl2;
}


public int getPreferredWidth() {
        return super.getPreferredWidth();

}
protected void layout(int width, int height) {
    super.layout(width, height);
    setExtent(getPreferredWidth(), getPreferredHeight());
}

protected void paint(Graphics graphics) {
int ypos = getContentHeight()/2;
graphics.drawText(lbl1, 0, ypos ,DrawStyle.LEFT | DrawStyle.VCENTER);
graphics.drawText(lbl2, 0, ypos ,DrawStyle.RIGHT | DrawStyle.VCENTER);
super.paint(graphics);
}

}

现在你必须在管理器中添加这个标签字段,如

HorizontalFieldManager hfm = new HorizontalFieldManager();
hfm.add(new LableFieldWithTwoLable("name","13") );

如果您需要任何其他功能,请根据需要在LableFieldWithTwoLable()类中添加构造函数。