【问题标题】:BlackBerry HorizontalFieldManager with two Buttons on Opposite SidesBlackBerry Horizo​​ntalFieldManager,对面有两个按钮
【发布时间】:2011-09-11 15:00:00
【问题描述】:

我有一个 Horizo​​ntalFieldManager,里面有两个按钮。这些按钮需要分别放在屏幕的最左侧和最右侧。我知道 Horizo​​ntalFieldManager 只将它们从左到右放置,所以我必须扩展它们。

这就是我所做的,但是它没有结果。按钮确实出现了,但太小而且上面没有文字(我已经设置好了)。

这是我的布局代码。

protected void sublayout(int width, int height) {
    setPositionChild(getField(0), 0, 0);        

    int fieldZeroWidth   = Max(getField(0).getContentWidth(),getField(0).getPreferredWidth());
    int fieldZeroHeight  = Max(getField(0).getContentHeight(),getField(0).getPreferredHeight());
    layoutChild(getField(0), 
            fieldZeroWidth, 
            fieldZeroHeight);
    int fieldOneWidth = Max(getField(1).getContentWidth(),getField(1).getPreferredWidth());
    setPositionChild(getField(1), 
            Display.getWidth() - fieldOneWidth,
            0);

    int fieldOneHeight = Max(getField(1).getPreferredHeight(),getField(1).getContentHeight());
    layoutChild(getField(1), 
            fieldOneWidth, 
            fieldOneHeight
            );

    setExtent(Display.getWidth(), Max(getPreferredHeight(),fieldOneHeight,fieldZeroHeight));
}

创建此自定义管理器后,我使用 setStatus() 函数将其添加到底部。

我希望这两个按钮显示在相对的两侧(它们确实如此),但是大小合适并显示它们的文本。

谢谢 显然,Max 函数只返回参数之间的最大值。

【问题讨论】:

  • 你的按钮是自定义的吗?

标签: java blackberry user-interface components horizontalfieldmanager


【解决方案1】:

如果要设置文本对齐,则必须为按钮域设置填充;如果要为按钮域设置对齐,则为按钮域设置边距

【讨论】:

    【解决方案2】:

    如果您想将两个字段放在 Horizo​​ntalFieldManager 的角落,请使用以下代码:

    import net.rim.device.api.ui.*;
    
    /**
     * Custom class to place the Fields on two corners of the screen Horizontally
     */
    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 );
        }
    
    
    
    }    
    

    【讨论】:

    猜你喜欢
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    相关资源
    最近更新 更多