具有自定义高度的 BlackBerry 垂直居中 LabelField

发布于 2024-11-09 16:41:27 字数 523 浏览 0 评论 0原文

我有一个嵌套在 TableLayoutManager 行中的 LabelField。我希望该行具有特定的高度(与其位图背景的高度相同)。为了实现这一点,我更改了嵌套 LabelField 的layout() 方法:

LabelField lblHours = new LabelField(hours,
            Color.BLACK, Manager.FIELD_VCENTER) {
        protected void layout(int width, int height) {
            super.layout(width, 40 //height of the bitmap);
            setExtent(width, 40 //height of the bitmap);
        }
    };

这成功地增加了 TableLayoutManager 行大小。但是,一旦我这样做,LabelField 就不再在行内垂直居中。关于如何做到这一点有什么建议吗?

谢谢!

I have a LabelField nested within a TableLayoutManager row. I want the row to be a specific height (the same height as its bitmap background). In order to achieve this, I changed the layout() method of the nested LabelField:

LabelField lblHours = new LabelField(hours,
            Color.BLACK, Manager.FIELD_VCENTER) {
        protected void layout(int width, int height) {
            super.layout(width, 40 //height of the bitmap);
            setExtent(width, 40 //height of the bitmap);
        }
    };

This successfully increased the TableLayoutManager row size. However, once I do this, the LabelField is no longer centered vertically within the row. Any suggestions on how to do that?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

许一世地老天荒 2024-11-16 16:41:27

上面的答案有效,只需确保您为上述内容输入两个语句即可。如下例所示。

LabelField importanceLabel = new LabelField("Importance: ",LabelField.FIELD_VCENTER | LabelField.FIELD_RIGHT) {
          public void paint(Graphics g) { 
              g.setColor(0x145085); 
              super.paint(g); 
          } 
          protected void layout(int width, int height) {
              super.layout(Math.min(width, this.getFont().getAdvance(this.getText())), 26); //height of the bitmap);
              setExtent(Math.min(width, this.getFont().getAdvance(this.getText())), 26); //height of the bitmap);
          }
      };

这将是相同的回应.. 谢谢你们.. 的讨论。它帮助了我。

The above answer works just make sure that you put in two statements for the above.. as illustrated in the below example.

LabelField importanceLabel = new LabelField("Importance: ",LabelField.FIELD_VCENTER | LabelField.FIELD_RIGHT) {
          public void paint(Graphics g) { 
              g.setColor(0x145085); 
              super.paint(g); 
          } 
          protected void layout(int width, int height) {
              super.layout(Math.min(width, this.getFont().getAdvance(this.getText())), 26); //height of the bitmap);
              setExtent(Math.min(width, this.getFont().getAdvance(this.getText())), 26); //height of the bitmap);
          }
      };

This is going to be the same response.. and thank you guys.. for the discussion. It helped me.

忆离笙 2024-11-16 16:41:27

尝试将 setExtent(width, 40) 更改为 setExtent(this.getWidth(), 40)。如果 getWidth() 尝试占用所有可用宽度(这就是您的 setExtent() 调用正在执行的操作),则这可能不起作用,并且由于文本将左对齐绘制,因此看起来您的字段不是'不居中,但实际上它只是占据了表格的整个单元格。或者,如果这不起作用,您可以执行 setExtent(Math.min(width, this.getFont().getAdvance(this.getText())), 40);

Try changing setExtent(width, 40) to setExtent(this.getWidth(), 40). This could possibly not work if getWidth() is trying to take up all of the available width (which is what your setExtent() call is doing), and since the text will be drawn left-aligned, it looks like your field isn't centered but in reality it's just taking up the entire cell of your table. Alternatively, if this doesn't work you may be able to do setExtent(Math.min(width, this.getFont().getAdvance(this.getText())), 40);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文