盒子¶
盒子是一种布局容器,为你的应用程序提供框架。你可以将盒子嵌套在其他盒子内部,以构建用户界面,并控制每个小部件如何相对于其父容器扩展和对齐。
盒子具有方向,可以是水平或垂直,并分别在同一行或列上排列其子部件。
同构盒子¶
默认情况下,盒子会根据方向为每个子部件提供其期望的大小。如果你希望每个子部件具有相同的大小,可以使用 GtkBox:homogeneous 属性
GtkWidget *box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
GtkWidget *hello = gtk_button_new_with_label ("Hello");
GtkWidget *gbye = gtk_button_new_with_label ("Goodbye");
gtk_box_append (GTK_BOX (box), hello);
gtk_box_append (GTK_BOX (box), gbye);
gtk_box_set_homogeneous (GTK_BOX (box), TRUE);
box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
hello = Gtk.Button(label="Hello")
gbye = Gtk.Button(label="Goodbye")
box.append(hello)
box.append(gbye)
box.props.homogeneous = True
var box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6);
var hello = new Gtk.Button.with_label ("Hello");
var gbye = new Gtk.Button.with_label ("Goodbye");
box.append (hello);
box.append (gbye);
box.homogeneous = true;
const box = new Gtk.Box({
orientation: Gtk.Orientation.HORIZONTAL,
spacing: 6,
});
const hello = new Gtk.Button({ label: "Hello" });
const gbye = new Gtk.Button({ label: "Goodbye" });
box.append(hello);
box.append(gbye);
box.homogeneous = true;
扩展盒子¶
如果子部件设置为扩展,盒子将为其提供额外的空间
GtkWidget *box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
GtkWidget *foo = gtk_button_new_with_label ("Live");
GtkWidget *bar = gtk_button_new_with_label ("Laugh");
GtkWidget *baz = gtk_button_new_with_label ("Love");
gtk_box_append (GTK_BOX (box), foo);
gtk_box_append (GTK_BOX (box), bar);
gtk_box_append (GTK_BOX (box), baz);
// We set the middle button to expand, which will make the
// box expand, if given more space
gtk_widget_set_hexpand (bar, TRUE);
box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
foo = Gtk.Button(label="Live")
# We set the middle button to expand, which will make the box
# expand, if given more space
bar = Gtk.Button(label="Laugh", hexpand=True)
baz = Gtk.Button(label="Love")
box.append(foo)
box.append(bar)
box.append(baz)
var box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6);
var foo = new Gtk.Button.with_label ("Live");
var bar = new Gtk.Button.with_label ("Laugh");
var baz = new Gtk.Button.with_label ("Love");
box.append (foo);
box.append (bar);
box.append (baz);
// We set the middle button to expand. which will make the box
// expand, if given more space
bar.hexpand = true;
const box = new Gtk.Box({
orientation: Gtk.Orientation.HORIZONTAL,
spacing: 6,
});
const foo = new Gtk.Button({ label: "Live" });
const bar = new Gtk.Button({ label: "Laugh" });
const baz = new Gtk.Button({ label: "Love" });
box.append(foo);
box.append(bar);
box.append(baz);
// We set the middle button to expand. which will make the box
// expand, if given more space
bar.hexpand = true;
<object class="GtkBox">
<property name="orientation">horizontal</property>
<property name="spacing">6</property>
<child>
<object class="GtkButton" id="foo">
<property name="label">Live</property>
</object>
</child>
<child>
<object class="GtkButton" id="bar">
<property name="label">Laugh</property>
<property name="hexpand">true</property>
</object>
</child>
<child>
<object class="GtkButton" id="baz">
<property name="label">Love</property>
</object>
</child>
</object>
组件的常用方法¶
set_baseline_position()方法控制盒子子部件的基线对齐方式;你可以指定当盒子从其父容器接收更多空间时,子部件的对齐方式。
API 参考¶
在示例中,我们使用了以下类