黑莓UI不同的设备

时间:2013-05-14 09:08:44

标签: user-interface blackberry

我正在开发一个黑莓应用程序。我有一个标题,你可以在下面的图片中看到,低分辨率,它占用的空间超过宽度,目前在BB Bold 9900模拟器上测试。所以我尝试使用以下代码来防止UI破坏。

问题

  1. 是否是阻止UI销毁的正确代码?
  2. 如果是,那么通过使用此代码,我们如何将按钮向右对齐,将徽标对齐到左侧。
  3. 我应该遵循哪种设计/ UI来防止不同分辨率设备中的UI破坏?

    ImageButton Login = new ImageButton(configModel.getLoginButton(), FOCUSABLE, "login.png", "plogin.png",0x9cbe95);
    HorizontalFieldManager hfm = new HorizontalFieldManager(Field.FIELD_HCENTER);
    HorizontalFieldManager kenexaLogoHfm = new HorizontalFieldManager(hfm.FIELD_LEFT);
    HorizontalFieldManager loginButtonHfm = new HorizontalFieldManager(hfm.FIELD_RIGHT);
    
    Bitmap logo = Bitmap.getBitmapResource("logo.png");
    NullField nullField = new NullField();
    BitmapField kenexaLogo = new BitmapField(logo);
    
    kenexaLogoHfm.add(new LabelField("", NON_FOCUSABLE));
    kenexaLogoHfm.add(kenexaLogo);
    kenexaLogoHfm.add(nullField);
    
    loginButtonHfm.add(Login);
    
    hfm.setPadding(0, 5, 0, 5);
    hfm.add(kenexaLogoHfm);
    hfm.add(loginButtonHfm)
    add(hfm);
    

    enter image description here

    以下是ImageButton的代码

    public class ImageButton extends Field{
    
            //Image Button Class 
            private String _label;
            private int _labelHeight;
            private int _labelWidth;
            private Font _font;
    
            private Bitmap _currentPicture;
            private Bitmap _onPicture;
            private Bitmap _offPicture;
            int color;
    
            public ImageButton(String text, long style ,String img, String img_hvr, int color){
                super(style);
    
                _offPicture = Bitmap.getBitmapResource(img);
                _onPicture = Bitmap.getBitmapResource(img_hvr);
    
                _font = Font.getDefault().derive(Font.BOLD, 7, Ui.UNITS_pt);
                _label = text;
    
    
                _labelHeight = _onPicture.getHeight();  
                _labelWidth = _onPicture.getWidth();
    
                this.color = color;
    
                _currentPicture = _offPicture;
            }
            public void setImage(String img){
    
                _offPicture = Bitmap.getBitmapResource(img); 
                _currentPicture = _offPicture;
            }
    
            /**
             * @return The text on the button
             */
            public void setText(String text){
                _label = text;
            }
                String getText(){
                return _label;
            }
    
            /**
             * Field implementation.
             * @see net.rim.device.api.ui.Field#getPreferredHeight()
             */
            public int getPreferredHeight(){
                return _labelHeight;
            }
            /**
             * Field implementation.
             * @see net.rim.device.api.ui.Field#getPreferredWidth()
             */
            public int getPreferredWidth(){
                return _labelWidth;
            }
    
            /**
             * Field implementation.  Changes the picture when focus is gained.
             * @see net.rim.device.api.ui.Field#onFocus(int)
             */
            protected void onFocus(int direction) {
    
                 _currentPicture = _onPicture;
              //  invalidate();
                super.onFocus(direction);
            }
    
            /**
             * Field implementation.  Changes picture back when focus is lost.
             * @see net.rim.device.api.ui.Field#onUnfocus()
             */
            protected void onUnfocus() {
                _currentPicture = _offPicture;
                invalidate();
                super.onUnfocus();
            }
    
            /**
             * Field implementation.  
             * @see net.rim.device.api.ui.Field#drawFocus(Graphics, boolean)
             */
    //      protected void drawFocus(Graphics graphics, boolean on) {
    //          // Do nothing
    //      }
            protected void drawFocus(Graphics graphics, boolean on) {
                if (on) {
                         //draw your own custom focus.
                        }
                    }
            /**
             * Field implementation.
             * @see net.rim.device.api.ui.Field#layout(int, int)
             */
            protected void layout(int width, int height) {
                setExtent(Math.min( width, getPreferredWidth()),
                Math.min( height, getPreferredHeight()));
            }
            /**
             * Field implementation.
             * @see net.rim.device.api.ui.Field#paint(Graphics)
             */
            protected void paint(Graphics graphics){      
                // First draw the background colour and picture
                graphics.setColor(this.color);
                graphics.fillRect(0, 0, getWidth(), getHeight());
                graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0, 0);
    
                // Then draw the text
                graphics.setColor(Color.WHITE);
                graphics.setFont(_font);
                graphics.setFont(graphics.getFont().derive(Font.BOLD)); 
                graphics.drawText(_label, 5,9,
                    (int)( getStyle() & DrawStyle.ELLIPSIS | DrawStyle.VALIGN_MASK | DrawStyle.HALIGN_MASK),
                    getWidth() - 6 );
    
            }
    
            /**
             * Overridden so that the Event Dispatch thread can catch this event
             * instead of having it be caught here..
             * @see net.rim.device.api.ui.Field#navigationClick(int, int)
             */
            protected boolean navigationClick(int status, int time){
                fieldChangeNotify(1);
                return true;
            } 
    }
    

1 个答案:

答案 0 :(得分:2)

我建议删除一些不必要的HorizontalFieldManager对象,并使用此代码:

  HorizontalFieldManager hfm = new HorizontalFieldManager(Field.USE_ALL_WIDTH);
  VerticalFieldManager vfm = new VerticalFieldManager(Field.USE_ALL_WIDTH);

  Bitmap logo = Bitmap.getBitmapResource("logo.png");
  BitmapField kenexaLogo = new BitmapField(logo, Field.FIELD_LEFT);

  hfm.add(new NullField());  // to take focus from login button
  hfm.add(kenexaLogo);
  vfm.add(new ButtonField("Login", Field.FIELD_RIGHT));
  hfm.add(vfm);

  hfm.setPadding(0, 5, 0, 5);
  add(hfm);

注意:我必须创建一个新的ButtonField,因为您没有显示您的登录按钮是如何创建的(原始问题中的 ),这一点非常重要。< / p>

问题是HorizontalFieldManager尝试使用它所使用的空间有效。因此,如果您只添加两个真实字段(徽标和按钮),并且这些字段不足以填充宽度,则不会将按钮放在右侧。

您需要向VerticalFieldManager添加HoriztonalFieldManager,告诉它使用所有可用宽度,然后将按钮传递给FIELD_RIGHT标志即可。字段的那些标志告诉他们的容器将它们放在哪里。 VerticalFieldManager会尊重FIELD_RIGHT,并根据您的意愿将登录按钮放在右侧。

更多

我可能还建议,不要硬编码5像素左右填充,而是设置一个填充常量,它是屏幕宽度的给定百分比:

int pad = Display.getWidth() / 100;
hfm.setPadding(0, pad, 0, pad);

但是,这是一个单独的问题。

在尝试调试这样的布局问题时,我觉得有用的另一件事是在布局中的每个管理器或字段上设置不同的背景颜色。这有助于您了解和了解正在发生的事情。只需使用:

hfm.setBackground(BackgroundFactory.createSolidBackground(Color.RED));  // TODO: remove!