0

是在焦点状态之前。它工作正常。

是对焦状态。它工作正常。

是在焦点状态之后。图像消失的地方发生了问题。

它适用于右上角,但左上角图像有问题。

这是我的习惯VerticalFieldManager

public class Custom_TopField extends HorizontalFieldManager implements
    FieldChangeListener {

private Bitmap bg = Bitmap.getBitmapResource("header_bar.png");
private Bitmap download = Bitmap.getBitmapResource("btn_download.png");
private Bitmap downloadactive = Bitmap
        .getBitmapResource("btn_download_active.png");
private Bitmap refresh = Bitmap.getBitmapResource("icon_refresh.png");
private Bitmap refreshactive = Bitmap
        .getBitmapResource("icon_refresh_active.png");
private Bitmap back = Bitmap.getBitmapResource("btn_back.png");
private Bitmap backctive = Bitmap.getBitmapResource("btn_back_active.png");
private Bitmap news = Bitmap.getBitmapResource("icon_news.png");
private Bitmap newsactive = Bitmap
        .getBitmapResource("icon_news_active.png");

private Custom_ButtonField downloadbtn, refreshbtn, backbtn, newsbtn;
private Custom_LabelField title;

Custom_TopField(final MainScreen mainscreen) {
    Background background = BackgroundFactory.createBitmapBackground(bg);
    setBackground(background);
    title = new Custom_LabelField("东方日报", DrawStyle.ELLIPSIS
            | LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER
            | Field.FOCUSABLE, Color.WHITE) {
        protected boolean navigationClick(int status, int time) {
            Main.getUiApplication().pushScreen(new Main_AllLatestNews());
            Main.getUiApplication().popScreen(mainscreen);
            return true;
        }
    };
    title.setFont(Font.getDefault().derive(Font.BOLD, 33));
    add(title);

    downloadbtn = new Custom_ButtonField(download, downloadactive,
            downloadactive);
    downloadbtn.setChangeListener(this);
    add(downloadbtn);

    refreshbtn = new Custom_ButtonField(refresh, refreshactive,
            refreshactive);
    refreshbtn.setChangeListener(this);
    add(refreshbtn);

    backbtn = new Custom_ButtonField(back, backctive, backctive);
    backbtn.setChangeListener(this);
    add(backbtn);

    /*newsbtn = new Custom_ButtonField(news, newsactive, newsactive);
    newsbtn.setChangeListener(this);
    add(newsbtn);*/
}

protected void sublayout(int width, int height) {
    Field field = getField(0);
    layoutChild(field, 120, Font.getDefault().getHeight());
    setPositionChild(field, (getPreferredWidth() - title.getWidth()) / 2,
            15);

    field = getField(1);
    layoutChild(field, download.getWidth(), download.getHeight());
    setPositionChild(field, getPreferredWidth()
            - (download.getWidth() + 10),
            getPreferredHeight() - (download.getHeight() + 5));

    field = getField(2);
    layoutChild(field, refresh.getWidth(), refresh.getHeight());
    setPositionChild(field,
            getPreferredWidth() - (refresh.getWidth() + 10),
            getPreferredHeight() - (refresh.getHeight() + 5));

    field = getField(3);
    layoutChild(field, back.getWidth(), back.getHeight());
    setPositionChild(field, 10, 5);

    /*field = getField(4);
    layoutChild(field, news.getWidth(), news.getHeight());
    setPositionChild(field, 10, 5);*/

    width = Math.min(width, getPreferredWidth());
    height = Math.min(height, getPreferredHeight());
    setExtent(width, height);
}

public int getPreferredHeight() {
    return 70;
}

public int getPreferredWidth() {
    return Display.getWidth();
}

public void paint(Graphics graphics) {
    int rectHeight = getPreferredHeight();
    int rectWidth = getPreferredWidth();

    graphics.drawRect(0, 0, rectWidth, rectHeight);
    super.paint(graphics);
}

public void fieldChanged(Field field, int context) {
    if (field == downloadbtn) {

    } else if (field == refreshbtn) {

    } else if (field == backbtn) {

    } else if (field == newsbtn) {

    }
}
}

这是custom button field

public class Custom_ButtonField extends ButtonField {
Bitmap mNormal;
Bitmap mFocused;
Bitmap mActive;

int mWidth;
int mHeight;

private int color = -1;
String text;

public Custom_ButtonField(Bitmap normal, Bitmap focused, Bitmap active) {
    super(CONSUME_CLICK | Field.FOCUSABLE);
    mNormal = normal;
    mFocused = focused;
    mActive = active;
    mWidth = mNormal.getWidth();
    mHeight = mNormal.getHeight();
    setMargin(0, 0, 0, 0);
    setPadding(0, 0, 0, 0);
    setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
    setBorder(VISUAL_STATE_ACTIVE,
            BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
}

public Custom_ButtonField(String text, Bitmap normal, Bitmap focused,
        Bitmap active, int color) {
    super(CONSUME_CLICK | Field.FOCUSABLE);
    this.color = color;
    mNormal = normal;
    mFocused = focused;
    mActive = active;
    mWidth = mNormal.getWidth();
    mHeight = mNormal.getHeight();
    setMargin(0, 0, 0, 0);
    setPadding(0, 0, 0, 0);
    setBorder(BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
    setBorder(VISUAL_STATE_ACTIVE,
            BorderFactory.createSimpleBorder(new XYEdges(0, 0, 0, 0)));
    this.text = text;
}

protected void onFocus(int direction) {
    super.onFocus(direction);
}

protected void onUnfocus() {
    super.onUnfocus();
}

protected void paint(Graphics graphics) {
    Bitmap bitmap = null;
    switch (getVisualState()) {
    case VISUAL_STATE_NORMAL:
        bitmap = mNormal;
        break;
    case VISUAL_STATE_FOCUS:
        bitmap = mFocused;
        break;
    case VISUAL_STATE_ACTIVE:
        bitmap = mActive;
        break;
    default:
        bitmap = mNormal;
    }
    graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(),
            bitmap, 0, 0);
    graphics.setFont(Font.getDefault().derive(Font.BOLD, 25));
    graphics.setColor(color);
    graphics.drawText(text, (mNormal.getWidth() - Font.getDefault()
            .getAdvance(text)) / 2, ((mNormal.getHeight() - Font
            .getDefault().getHeight()) / 2) + 10, DrawStyle.HCENTER
            | DrawStyle.VCENTER);
}

public int getPreferredWidth() {
    return mWidth;
}

public int getPreferredHeight() {
    return mHeight;
}

protected void layout(int width, int height) {
    setExtent(mWidth, mHeight);
}
}
4

1 回答 1

0

这是我在过去几周看到的第二个问题。

背景

要了解解决方案,首先您应该了解 BlackBerry 中的基本 UI 类。

首先,我们Field。AField是普通 UI组件的基类。如果您自己编写一个 UI 组件,从头开始,那么您将子类化Field

public class MyWidget extends Field {

但是,如果已经存在一个 BlackBerry 类几乎可以满足您的需求,并且您只需要稍微改变它的行为,那么您将子类化其他东西。例如:

public class MyButtonWidget extends ButtonField {

Manager也存在相同的模式。如果您是Manager 从头开始编写,请扩展Manager

public class MyManager extends Manager {

根据黑莓文档,这涉及到这样做:

实现自己的布局管理器

如果您有特殊需求,您可以实现自己的管理器。扩展Manager类,实现子布局、getPreferredWidth、getPreferredHeight。为了提高效率,您可以选择覆盖 subpaint。

但是,如果现有的 Manager子类已经完成了您需要的大部分工作,并且您只想自定义它,那么您可能会考虑扩展该子类:

public class MyHorizontalManager extends HorizontalFieldManager {

在您的情况下,您Custom_TopField正在为完全自定义完成所有Manager必需的工作(请参阅上面 javadocs 中突出显示的引用)。因此,您实际上没有任何理由扩展 HorizontalFieldManager. HorizontalFieldManager当您只想要add()您的字段并将它们全部水平布置时,使用A。但是,您在sublayout()代码中明确地执行此操作。事实证明,您的逻辑似乎正在与基类竞争。

解决方案

所以,你应该做的是让你的课程扩展Manager

public class Custom_TopField extends Manager implements FieldChangeListener {

如果这样做,您将需要调用不同的超级构造函数。像这样的东西(您可能想根据需要选择不同的样式常量):

Custom_TopField(final MainScreen mainscreen) {
   super(Manager.USE_ALL_WIDTH | Manager.NO_VERTICAL_SCROLL | Manager.NO_HORIZONTAL_SCROLL);

另一种选择是根本不实现sublayout(),像原来那样扩展HorizontalFieldManager,然后使用子字段的边距long 样式标志控制布局。但是,由于我上面给出的解决方案只需要更改 2 行代码,这对你来说可能是最简单的。

其他问题

我还在您的代码和屏幕截图中注意到“下载”按钮没有出现。我不知道所有 png 图像的确切大小,但是如果刷新和下载图像的大小相同,那么您当前的逻辑就是将刷新按钮放在下载按钮的正上方。所以,下载按钮是隐藏的。这可能不是你想要的?

于 2012-07-06T07:19:18.573 回答