gpt4 book ai didi

blackberry - labelField.getWidth() 返回 0?为什么会这样?

转载 作者:行者123 更新时间:2023-12-02 22:34:02 29 4
gpt4 key购买 nike

我正在开发我自己的自定义管理器,到目前为止我已经完成了它,但是它使用屏幕分辨率的百分比来设置边距。

下面是我如何调用下面的类:

LabelIconCommandManager licm3 = new LabelIconCommandManager("Address blah bklahblah ", 0);
licm3.add(new ImageButtonField(b1, b2, b3, Field.FIELD_LEFT | ImageButtonField.CONSUME_CLICK));

这是类 [我在评论中标记了返回 0 和返回 219 的地方。请告诉我为什么会这样:

public class LabelIconCommandManager extends HorizontalFieldManager implements  BCMSField
{
LabelIconCommandManager me = this;
EvenlySpacedHorizontalFieldManager buttonManager = new EvenlySpacedHorizontalFieldManager(0);
LabelField labelField;
int side = 0;
int HPADDING = 3;
int VPADDING = 4;
int screenWidth = Display.getWidth();
int labelField_width = 40;
public LabelIconCommandManager()
{
this("", 0);
}
public LabelIconCommandManager(String label, long style)
{
super(USE_ALL_WIDTH| FOCUSABLE);
this.setBorder(BorderFactory.createBitmapBorder(new XYEdges(15, 20, 15, 20),Bitmap.getBitmapResource( "border_edit.png" )));
this.setMargin(1,10,1,10);
labelField = new LabelField(label,LabelField.ELLIPSIS)
{
public void layout(int width, int height)
{
// Done because otherwise ellipses dont work with labelfields
super.layout((int)(screenWidth * 0.61), getHeight());
setExtent((int)(screenWidth * 0.61), getHeight());
labelField_width = labelField.getWidth();
DisplayDialog.alert("labelField_width = " + labelField_width); // returns 219
}
};
// Top Right Bottom Left
labelField.setMargin(VPADDING, HPADDING, VPADDING, 0);
// super because we want this horizontalfieldManager to add it
super.add(labelField);
super.add(buttonManager);
}
public void alternateConstructor(Attributes atts)
{
labelField = new LabelField(atts.getValue("label"), 0);
}
public void onFocus(int direction)
{
this.setBorder(BorderFactory.createBitmapBorder(new XYEdges(15, 20, 15, 20),Bitmap.getBitmapResource( "border_edit_select.png" )));
// uses the same color as listStyleButtonField selections
this.setBackground(BackgroundFactory.createSolidBackground(0x186DEF));
super.onFocus(direction);
}
//Invoked when a field loses the focus.
public void onUnfocus()
{
//top, right,bottom,left
this.setBorder(BorderFactory.createBitmapBorder(new XYEdges(15, 20, 15, 20),Bitmap.getBitmapResource( "border_edit.png" )));
this.setBackground(BackgroundFactory.createSolidTransparentBackground(Color.GRAY, 0));
super.onUnfocus();
invalidate();
}
// Overrride this managers add function
public void add(Field imageButton)
{
// Add a button to the evenly spaced manager
buttonManager.add(imageButton);
// Based on how many buttons there are, set the margin of where the manager holding the buttons start [offset from labelField]
if(buttonManager.getFieldCount() == 1)
{
//side = (int)(screenWidth * 0.1388);
side = screenWidth - labelField_width - 32 - 10 - 15;
DisplayDialog.alert("Screen Width = " + screenWidth);
DisplayDialog.alert("labelField_width2 = " + labelField_width); // returns 0
DisplayDialog.alert("Side = " + side);
}
else side = (int)(screenWidth * 0.05);
buttonManager.setMargin(0,0,0,side);
}
public int getLabelWidth()
{
return labelField_width;
}
}

为了更清楚,这里有一张图片:

Snapshot

最佳答案

注意:当我运行您的代码时,我实际上并没有看到 labelField_width 设置为 0。您在上面发布的代码中将该值初始化为 40。因此,我有时会看到它设置为 40 或 219(在 360 像素宽的屏幕上)。

但是,问题是我认为您过早地试图访问 labelField_width 的值。它唯一正确分配的位置是在匿名 LabelFieldlayout() 方法中。仅仅因为您根据实例化声明并实现了 layout() 方法,并不意味着它在创建 LabelField 时被调用。这其实也是我不喜欢匿名类的原因之一。

无论如何,这段代码:

LabelIconCommandManager licm3 = new LabelIconCommandManager("Address blah bklahblah ", 0); 
licm3.add(new ImageButtonField(b1, b2, b3, Field.FIELD_LEFT | ImageButtonField.CONSUME_CLICK));

将首先实例化 LabelField(在 LabelIconCommandManager 构造函数内)。正如我所说,这不会触发layout() 方法。上面的第二行 (add()) 将触发您重写的方法:

// Overrride this managers add function 
public void add(Field imageButton)
{

这是您看到 labelField_width 错误值的地方。该方法在 layout() 之前被调用。这就是问题所在。

因为看起来您只使用该宽度来设置 buttonManager 边距,所以您可以稍等片刻。如果您等到 LabelIconCommandManager sublayout() 方法被调用,您的 LabelField 将有其 layout()调用方法,并正确分配 labelField_width:

protected void sublayout(int maxWidth, int maxHeight) {
// make sure to call superclass method first!
super.sublayout(maxWidth, maxHeight);

// now, we can reliably use the label width:
side = screenWidth - labelField_width - 32 - 10 - 15;
buttonManager.setMargin(0,0,0,side);
}

该方法位于 LabelIconCommandManager 类中。然后,您可以删除调用 buttonManager.setMargin() 的其他地方。

关于blackberry - labelField.getWidth() 返回 0?为什么会这样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11747567/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com