我是 Linux 开发和 Vala/Gtk 的新手。我正在尝试在测试各种功能时尽可能多地学习。
这一次,我尝试创建一些 GUI 应用程序,但我坚持设置小部件的高度。查看 valadoc 和 gnome 文档,我找不到任何方法来这样做。
例如,如果我在一个水平框上有两个按钮,我应该如何调整 Box()
的高度?
这是我现在的代码:
var hBox = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
hBox.set_spacing(-10);
hBox.set_homogeneous(true); // Same width of buttons
// Set margins of maim menu icons
hBox.set_margin_start(30);
hBox.set_margin_end(30);
hBox.set_margin_top(10);
// Top buttons (Main | Notifications)
var btnMain = new Gtk.Button.with_label("Main");
var btnNotif = new Gtk.Button.with_label("Notifications");
nixnMain.add(hBox);
hBox.pack_start(btnMain);
hBox.pack_start(btnNotif);
我现在已经尝试了几个选项,包括 set_margin_bottom()
、get_request_mode()
和 size_allocate()
,还有 get_preffered_size ()
和文档页面中的其他几个。
此外,我如何指定小部件“类”,例如,如果我的 CSS 如下所示,我如何指定按钮具有类 buttonMain?
#buttonMain {
background-color: black;
}
谢谢!
编辑: 图片提供
使用 hBox.set_margin_bottom(Screen.height() + 50);
我得到了如下图所示的结果,但我希望灰色按钮的高度更小。
Image
最佳答案
您不应该直接设置小部件的高度。您永远不知道用户什么时候可能会设置不同的字体大小(例如,用于无障碍技术),而您的高度会被证明是错误的。相反,使用 heexpand
/halign
/vexpand
/valign
/margin-top
/margin-bottom
/margin-right
/margin-left
属性,或小部件容器的 border-width
或间距属性,或使用 CSS 中的填充属性。
关于linux - Vala/GTK3 设置控件高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34971276/