如何完全设置背景图像填充屏幕?

时间:2013-01-24 21:58:51

标签: java blackberry

我有一个背景图片,并在一个屏幕上叠加了2个LabelFields,一个BasicEditField和一个ButtonField。我的问题是我的背景图像在按钮底部之后切断,而不是完全填满屏幕。 (为了便于阅读,我删除了多余的代码)

    VerticalFieldManager bgManager = new VerticalFieldManager(Manager.USE_ALL_WIDTH | 
                                                              Manager.NO_VERTICAL_SCROLL | 
                                                              Manager.NO_VERTICAL_SCROLLBAR) {

        //Override the paint method to draw the background image.
        public void paint(Graphics graphics)
        {
            //Draw the background image and then call super.paint
            //to paint the rest of the screen.
            graphics.drawBitmap(0, 0, Display.getWidth(), Display.getHeight(), backgroundBitmap, 0, 0);
            super.paint(graphics);
        }
    };
    this.add(bgManager);

    LabelField header = new LabelField("", LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER);
    header.setText("Search by Drug Name");
    bgManager.add(header);

    LabelField subtitle = new LabelField("", LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER);
    subtitle.setText("(brand name or generic)");
    bgManager.add(subtitle);

    HorizontalFieldManager hfm = new HorizontalFieldManager();

    final BasicEditField edit = new BasicEditField("", "", 50, EditField.EDITABLE | BasicEditField.NO_NEWLINE) {
        //code to make background of text box white
        }
    };
    hfm.add(edit);

    ButtonField search = new ButtonField("Search");
    search.setChangeListener(new FieldChangeListener() {

        public void fieldChanged(Field field, int context) {
            //do something
        }
    });

如果我将Manager.USE_ALL_HEIGHT添加到bgManager,我的背景图片会完全填满整个屏幕,但我的整个屏幕都会无限滚动。没有Manager.USE_ALL_HEIGHT,我的屏幕根本不可滚动(正确),但背景图像会在内容结尾处切断。

如何构建此屏幕,使其不可滚动,并且背景图像占据整个可见屏幕?

这是错误的:

enter image description here

这是正确的(但滚动不正确):

enter image description here

这是我正在使用的背景图片:

enter image description here

1 个答案:

答案 0 :(得分:1)

我认为更改代码的最简单方法是使用USE_ALL_HEIGHT标记。

  VerticalFieldManager bgManager =
    new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | 
                             Manager.NO_VERTICAL_SCROLLBAR | 
                             Manager.USE_ALL_WIDTH | 
                             Manager.USE_ALL_HEIGHT) {

这将使您的屏幕充满背景图片。

您没有显示此代码,但我猜测问题不是bgManager实际上允许滚动,而是它所在的Screen允许垂直滚动。因此,请确保您放入代码的容器禁用滚动。像这样:

public class BgCropScreen extends MainScreen {

   public BgCropScreen() {
      super(MainScreen.NO_VERTICAL_SCROLL);  // <<<<<< THIS IS THE CRITICAL LINE !!!!!!

      VerticalFieldManager bgManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR | Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT) {
         // to draw the background image.
         public void paint(Graphics graphics) {
            graphics.drawBitmap(0, 0, Display.getWidth(), Display.getHeight(), backgroundBitmap, 0, 0);
            super.paint(graphics);
         }                
      };
      this.add(bgManager);

Here is the documentation for doing this。它指出,在较新版本的操作系统中,使用位图,渐变,颜色等使用BackgroundFactory方法创建背景可能更容易。但是,这只是另一种选择。