1

我想知道,如何在黑莓上调整我的制表栏。因为我的标签与黑莓 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);
}


图片背景按钮字段类

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);
}
4

1 回答 1

1

你没有向我们展示你有什么样的标签栏背景,解决方案确实有点依赖于此。如果您很高兴拥有一个始终具有相同高度(以像素为单位)的标签栏,但只是更改了width,那么您可以使用类似的东西。

我创建了一个名为 Manager 的子类TabBarManager。它将跨越屏幕的整个宽度,并具有固定的高度。它可以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(高度必须相同...宽度无关紧要):

在此处输入图像描述, 在此处输入图像描述,在此处输入图像描述

你如何使用它

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

限制

上面的实现只会拉伸标签栏的宽度。高度是固定的。对于完全可拉伸的标签栏,您可以通过绘制 9 个图像(左上、中上、右上、左、中、右、左下、中下、右下)来模仿9 块图像)。或者使用类似的东西来为 BlackBerry 获取 9-patch 可拉伸图像

参考

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

于 2013-02-23T04:47:49.730 回答