黑莓自定义选项卡适合所有设备屏幕

时间:2013-02-22 07:24:54

标签: blackberry java-me

我想知道,如何在黑莓上安装我的制表栏。因为我的标签与黑莓9700匹配,但对于黑莓9900,我的标签太小了。我想我的标签适合所有设备碎片。
提前谢谢:)

这是代码,我是从其他帖子得到的。抱歉:
BottomPanel分类

public class BottomPanel extends VerticalFieldManager implements
    FieldChangeListener {

Bitmap home_bit = Bitmap.getBitmapResource("home.png");
Bitmap home_bit_hover = Bitmap.getBitmapResource("home_h.png");
Bitmap map_bit = Bitmap.getBitmapResource("map.png");
Bitmap map_bit_hover = Bitmap.getBitmapResource("map_h.png");
Bitmap contact_bit = Bitmap.getBitmapResource("contact.png");
Bitmap contact_bit_hover = Bitmap.getBitmapResource("contact_h.png");
PictureBackgroundButtonField home_pic, map_pic, contact_pic;

HorizontalFieldManager hr;
int current_index = 0;

public BottomPanel(int current_index) {
    super(FOCUSABLE);
    this.current_index = current_index;
    VerticalFieldManager ver = new VerticalFieldManager(USE_ALL_WIDTH
            | USE_ALL_HEIGHT) {
        protected void sublayout(int width, int height) {
            super.sublayout(width, home_bit.getHeight());
            setExtent(width, home_bit.getHeight());
        }
    };
    hr = new HorizontalFieldManager(FIELD_HCENTER);
    if (current_index == 1) {
        home_pic = new PictureBackgroundButtonField(home_bit.getWidth(),
                home_bit.getHeight(), Field.NON_FOCUSABLE
                        | Field.FIELD_VCENTER, home_bit_hover,
                home_bit_hover);
    } else {
        home_pic = new PictureBackgroundButtonField(home_bit.getWidth(),
                home_bit.getHeight(),
                Field.FOCUSABLE | Field.FIELD_VCENTER, home_bit,
                home_bit_hover);
    }
    home_pic.setChangeListener(this);
    hr.add(home_pic);

    if (current_index == 2) {
        map_pic = new PictureBackgroundButtonField(map_bit.getWidth(),
                map_bit.getHeight(), Field.NON_FOCUSABLE
                        | Field.FIELD_VCENTER, map_bit_hover, map_bit_hover);
    } else {
        map_pic = new PictureBackgroundButtonField(map_bit.getWidth(),
                map_bit.getHeight(), Field.FOCUSABLE | Field.FIELD_VCENTER,
                map_bit, map_bit_hover);
    }
    map_pic.setChangeListener(this);
    hr.add(map_pic);

    if (current_index == 3) {
        contact_pic = new PictureBackgroundButtonField(
                contact_bit.getWidth(), contact_bit.getHeight(),
                Field.NON_FOCUSABLE | Field.FIELD_VCENTER,
                contact_bit_hover, contact_bit_hover);
    } else {
        contact_pic = new PictureBackgroundButtonField(
                contact_bit.getWidth(), contact_bit.getHeight(),
                Field.FOCUSABLE | Field.FIELD_VCENTER, contact_bit,
                contact_bit_hover);
    }
    contact_pic.setChangeListener(this);
    hr.add(contact_pic);

    ver.add(hr);
    add(ver);
}

public void fieldChanged(Field field, int context) {
    if (field == home_pic) {
        LoadingScreen loadingScreen = new LoadingScreen(1);
        UiApplication.getUiApplication().popScreen(
                UiApplication.getUiApplication().getActiveScreen());
        UiApplication.getUiApplication().pushScreen(loadingScreen);
        loadingScreen.createGUI();
    } else if (field == map_pic) {
        LoadingScreen loadingScreen = new LoadingScreen(2);
        UiApplication.getUiApplication().popScreen(
                UiApplication.getUiApplication().getActiveScreen());
        UiApplication.getUiApplication().pushScreen(loadingScreen);
        loadingScreen.createGUI();
    } else if (field == contact_pic) {
        LoadingScreen loadingScreen = new LoadingScreen(3);
        UiApplication.getUiApplication().popScreen(
                UiApplication.getUiApplication().getActiveScreen());
        UiApplication.getUiApplication().pushScreen(loadingScreen);
        loadingScreen.createGUI();
    }
}


加载屏幕类

public class LoadingScreen extends MainScreen {
private LabelField text;
private LabelField texthasil;
private VerticalFieldManager manager;

int current_index = 0;
BottomPanel bottomPanel;

public LoadingScreen(int current_index) {

    this.current_index = current_index;
    bottomPanel = new BottomPanel(current_index);
    setStatus(bottomPanel);
}

public void createGUI() {

    manager = new VerticalFieldManager(Manager.VERTICAL_SCROLL
            | Manager.VERTICAL_SCROLLBAR);


    setStatus(bottomPanel);
}


PictureBackgroundButtonField类

public class PictureBackgroundButtonField extends Field {
private String _label;
private int _labelHeight;
private int _labelWidth;
private Font _font;

private Bitmap _currentPicture;
private Bitmap _onPicture;
private Bitmap _offPicture;

public PictureBackgroundButtonField(int width, int height, long style,
        Bitmap picture, Bitmap selectedPic) {
    super(style);

    _font = getFont();
    _label = "";
    _labelHeight = height;
    _labelWidth = width;
    _currentPicture = picture;
    _onPicture = selectedPic;
    _offPicture = picture;
}

protected void drawFocus(Graphics graphics, boolean on) {
    // Do nothing
}

public int getPreferredHeight() {
    return _labelHeight;
}

public int getPreferredWidth() {
    return _labelWidth;
}

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

protected boolean navigationClick(int status, int time) {
    fieldChangeNotify(1);
    return true;
}

protected void onFocus(int direction) {
    _currentPicture = _onPicture;
    invalidate();
}

protected void onUnfocus() {
    _currentPicture = _offPicture;
    invalidate();
}

protected void paint(Graphics graphics) {
    graphics.drawBitmap(0, 0, getPreferredWidth(), getPreferredHeight(),
            _currentPicture, 0, 0);
    graphics.setFont(_font);
    graphics.drawText(
            _label,
            4,
            2,
            (int) (getStyle() & DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK),
            getWidth() - 6);
}

1 个答案:

答案 0 :(得分:1)

您没有向我们展示您拥有哪种标签栏背景,解决方案确实依赖于此。如果您很高兴标签栏总是相同高度(以像素为单位),但只是更改宽度,那么您可以使用类似的内容。

我创建了一个名为TabBarManager的Manager子类。它将跨越屏幕的整个宽度,具有固定的高度。它可以像任何普通管理器一样添加Field个对象。它旨在添加按钮字段,这样当您单击按钮字段时,会发生一些事情。您可能还希望更改按钮字段的外观,具体取决于选择的选项卡。但是,目前尚不清楚这个问题是关于那个问题的,所以我没有显示该代码。所有这些代码都是为您提供一个管理器来添加选项卡字段,这将绘制一个全角背景。

您添加到此的标签栏字段应包含具有透明背景的图标图像和/或标签。例如,如果选项卡是地图视图,则为地球的白色轮廓图标。透明背景显示到TabBarManager背景。

该技术是绘制(在Photoshop中,或其他任何)三个图像。 中心图像。考虑绘制完整的标签栏图像。然后,裁掉左边的几个像素,并保存为TabBar-left.png。裁剪正确的几个像素并保存为TabBar-right.png,然后从中心裁剪几个像素,并另存为TabBar-center.png。示例图像显示在代码下方:

   /**
    * A TabBarManager provides a horizontal bar of button fields, that serve as a tab bar 
    * header or footer, used to select between available subviews in a larger Screen.
    */
   private final class TabBarManager extends HorizontalFieldManager {

      private int height;
      private Bitmap left;
      private Bitmap center;
      private Bitmap right;

      public TabBarManager() {
         super(HorizontalFieldManager.NO_VERTICAL_SCROLL);  // tab bar itself doesn't scroll
         left = Bitmap.getBitmapResource("TabBar-left.png");
         right = Bitmap.getBitmapResource("TabBar-right.png");
         center = Bitmap.getBitmapResource("TabBar-center.png");
         height = left.getHeight();
      }

      public void sublayout(int width, int h) {
         super.sublayout(width, height);
         setExtent(width, height);   // restrict height to a fixed value
      }

      public int getPreferredHeight() {
         return height;
      }

      public void paint(Graphics g) {
         // draw the background image for the tab bar with two sides and a center section, 
         //  to account for the fact that different devices have different widths
         int width = Display.getWidth();         
         g.drawBitmap(0, 0, left.getWidth(), height, left, 0, 0);

         // fill in the center by repeating the center image as many times as needed
         int x = left.getWidth();
         int centerWidth = center.getWidth();
         int leftEdgeOfRightBitmap = width - right.getWidth();
         while (x < leftEdgeOfRightBitmap) {
            g.drawBitmap(x, 0, centerWidth, height, center, 0, 0);
            x += centerWidth;
         }

         // draw right side
         g.drawBitmap(leftEdgeOfRightBitmap, 0, right.getWidth(), height, right, 0, 0);

         // use super.paint() to draw the icons/labels on top of our background
         super.paint(g);
      }
   }

左,中,右PNG(必须高度相同......宽度无关紧要):

enter image description hereenter image description hereenter image description here

您如何使用

在您显示的代码中,您可以将hr变量替换为我的TabBarManager实例。或者您可以将我的TabBarManager类重命名为BottomPanel,并添加您需要的其他代码......例如当前索引和字段更改侦听器回调。

限制

上述实现只会拉伸标签栏的宽度。高度是固定的。对于完全可伸缩的标签栏,您可以通过绘制9个图像(左上角,顶部中心,右上角,左侧,中间,右侧,左下角,底部中心,底部)来模仿9-patch image。对)。或者使用something like this to get 9-patch stretchable images for BlackBerry

参考

http://supportforums.blackberry.com/t5/Java-Development/Create-tabbed-view-screens/ta-p/444969