【问题标题】:Separating Gtk.Grid into two rows将 Gtk.Grid 分成两行
【发布时间】:2015-07-27 17:12:47
【问题描述】:

我想在我的Gtk.Grid 中有两个“行”。

每一行都有它的元素。顶行将有几个彼此相邻的标签,而底行将有两个彼此相邻的按钮。两行中的元素都应该在屏幕上居中。

我之前没有任何与 Vala 合作的经验,但我认为我可以这样做:

//declaration of the grid
var layout = new Grid ();
layout.orientation = Gtk.Orientation.VERTICAL;

//declaration of labels
...

//attaching of labels
layout.attach (label1, 0, 0, 1, 1);
layout.attach_next_to (label2, label1, Gtk.PositionType.RIGHT, 1, 1);
layout.attach_next_to (label3, label2, Gtk.PositionType.RIGHT, 1, 1);
layout.attach_next_to (label4, label3, Gtk.PositionType.RIGHT, 1, 1);
layout.attach_next_to (label5, label4, Gtk.PositionType.RIGHT, 1, 1);

//declaration of buttons
...

layout.attach (startBtn, 0, 0, 1, 1);
layout.attach_next_to (stopBtn, startBtn, Gtk.PositionType.RIGHT, 1, 1);

但看起来它不是那样工作的。我试图找到一些示例,但它们旁边似乎都有一堆代码,所以现在我已经筋疲力尽了,我仍然找不到解决这个问题的方法。

编辑:我得到的结果如下所示。

【问题讨论】:

  • 您发布的代码会发生什么情况?您似乎几乎拥有它,但我不完全确定您缺少什么。
  • @andlabs 我添加了显示我得到的结果的屏幕截图。
  • 啊,我明白了。 attach()0, 0 参数是要附加到的单元格。您正在更换控件。 (您可能应该在 stderr 上收到警告;如果没有……)请参阅 this

标签: gtk vala


【解决方案1】:

这是一篇关于这个主题的非常好的文章:http://www.abenga.com/post/2014/11/10/layout-widgets/

正如 andlabs 指出的,我会使用 public void attach (Widget child, int left, int top, int width = 1, int height = 1) http://valadoc.org/#!api=gtk+-3.0/Gtk.Grid.attach1

layout.attach (label1, 0, 0, 1, 1);
layout.attach (label2, 1, 0, 1, 1);
layout.attach (label3, 0, 1, 1, 1);
layout.attach (label4, 1, 1, 1, 1);

这样的事情

-------------------
| label1 | label2 |
-------------------
| label3 | label4 |
-------------------

【讨论】:

    猜你喜欢
    • 2018-04-18
    • 2016-03-26
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    相关资源
    最近更新 更多