我想将自定义按钮字段(屏幕截图上的星号)与水平管理器的中心对齐
HFM 的定义
final HorizontalFieldManager _navigation = new HorizontalFieldManager(HorizontalFieldManager.NO_VERTICAL_SCROLL | HorizontalFieldManager.FIELD_VCENTER | Field.USE_ALL_WIDTH)
{
protected void paint(Graphics graphics)
{
graphics.setGlobalAlpha(100);
int[] xInds = new int[]{0, getExtent().width, getExtent().width, 0};
int[] yInds = new int[]{0, 0, getExtent().height, getExtent().height};
final int[] cols = new int[]{Color.WHITE, Color.WHITE, color_computacenter_light_blue, color_computacenter_light_blue};
graphics.drawShadedFilledPath(xInds, yInds, null, cols, null);
super.paint(graphics);
}
protected void sublayout(int maxWidth, int maxHeight)
{
super.sublayout(maxWidth, maxHeight);
setExtent(maxWidth, navigation_vertical_manager_height);
}
};
自定义字段(部分)
public class NavigationButton extends Field
{
private String label;
Bitmap img;
int horizontalPadding = 4;
int verticalPadding = 10;
static int height;
static int weight;
ToolTip _tooltip;
public NavigationButton(String name, Bitmap img)
{
label = name;
this.img = img;
this.height = img.getHeight() + 4;
this.weight = img.getWidth() + 2;
}
public NavigationButton(String name, Bitmap img, long style)
{
super(style);
label = name;
this.img = img;
this.height = img.getHeight() + 4;
this.weight = img.getWidth() + 2;
}
protected void layout(int maxWidth, int maxHeight)
{
setExtent(maxWidth, maxHeight);
// setExtent(Math.min(weight, maxWidth), Math.min(height, maxHeight));
}
protected void paint(Graphics graphics)
{
// Draw background
graphics.setGlobalAlpha(255);
if(isFocus())
{
graphics.setColor(0x005AA1);
graphics.drawRoundRect(0 ,0 , weight + 1, height + 1, 2, 2);
}
if (img != null)
{
graphics.drawBitmap(0, 0 , img.getWidth(), img.getHeight(), img, 0, 0);
}
}
[...]
任何想法,我做错了什么?!
谢谢!
最佳答案
我还没有测试过这段代码,但我怀疑你的问题出在你的领域的布局方法上:
protected void layout(int maxWidth, int maxHeight)
{
setExtent(maxWidth, maxHeight);
// setExtent(Math.min(weight, maxWidth), Math.min(height, maxHeight));
}
您评论该行是否有原因?我认为正在发生的事情是您的自定义字段占据了整个宽度,并且在您的绘画方法中您在最左边绘制位图,使其看起来像字段左对齐。如果您取消注释该行,并在构建字段时添加 Field.FIELD_HCENTER 字段样式位,它应该可以工作。
或者,您可以保持布局不变,只需将位图居中绘制,方法是将 paint 方法中的线条更改为类似这样的内容
graphics.drawBitmap((this.getWidth()-img.getWidth())/2, 0 , img.getWidth(), img.getHeight(), img, 0, 0);
字段仍会占据整个宽度,但图像会居中。这可能会导致触摸屏设备(即 Storm)出现意外行为。
关于java - Blackberry - 字段 - 水平对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1093328/