跳转到内容

横排

横排,子组件垂直居中。

ui.row

基本用法

ui.row().gap(8).children(|ui| {
ui.button("Cancel");
ui.button("Save").primary();
});

垂直于排列方向的对齐

// Rows center their children; `items_start` and `items_end` move them.
ui.row().gap(8).items_end().children(|ui| {
ui.button("Large").large();
ui.button("Default");
ui.button("Small").small();
});

两端分布

ui.row().w_fill().justify_between().children(|ui| {
ui.text("3 files selected").muted();
ui.button("Clear").small();
});

居中和靠终点

ui.column().w_fill().p(8).gap(8).bordered().rounded_md().children(|ui| {
ui.row().w_fill().justify_center().children(|ui| {
ui.button("Centered").outline();
});
ui.row().w_fill().gap(8).justify_end().children(|ui| {
ui.button("Cancel");
ui.button("Save").primary();
});
});

不同字号的文字落在同一条基线上

ui.row().gap(6).items_baseline().children(|ui| {
ui.title("1,284");
ui.text("files").muted();
});

自身对齐、均匀分布

ui.column().w_fill().p(12).gap(12).bordered().rounded_md().children(|ui| {
ui.row().w_fill().justify_evenly().muted().children(|ui| {
ui.text("Overview");
ui.text("Files");
ui.text("Settings");
});
ui.separator();
ui.button("Done").outline().small().self_end();
});

空间不够时谁先让步

ui.row().w(260).px(12).py(8).gap(8).bordered().rounded_md().children(|ui| {
ui.icon(IconName::File);
// The file name stays whole; the path truncates first.
ui.text("notes-2026.md").truncate().layout_priority(1.0);
ui.text("~/Documents/work/archive/2026").text_sm().truncate().muted();
});

接口

设置说明
.style(style: Style)替换整个样式。之后调用的方法在它的基础上调整。
.rounded_sm()主题的 radius_sm。
.rounded_md()主题的控件圆角 radius。
.rounded_lg()主题的 radius_lg。
.rounded_full()两端为半圆。
.bordered()1 逻辑像素宽的边框,颜色为主题的 border。
.bg_muted()用主题的 muted 色填充,适合不显眼的区域。
.bg_surface()用主题的 surface 色填充,和卡片、弹出层一样。
.shadow_sm()小阴影,颜色取自主题。
.shadow_md()中等阴影,颜色取自主题,和卡片一样。
.shadow_lg()大阴影,颜色取自主题,和弹出框、对话框一样。
.text_color(color: Color)里面文字和图标的颜色;它们自己设了颜色的除外。
.muted()里面的文字和图标使用 muted_foreground 色。
.text_sm()里面的文字使用主题的小字号。
.text_size(size: impl Length)里面文字的字号,单位为逻辑像素。
.disabled(disabled: bool)为真时停用里面的所有组件:变淡,不响应指针和键盘,Tab 跳过。
.when(condition: bool, f: impl FnOnce(Self)condition 为真时应用 f。
.when_some(value: Option<T>, f: impl FnOnce(Self, T)有值时对该值应用 f。
.hover(f: impl FnOnce(Style)指针在容器或里面的组件上时的外观。f 可以改底色、边框、圆角、阴影、焦点环或不透明度,变化按主题的动效时长过渡。要在其他样式方法之后调用。滚动区域和浮层会忽略它。
.active(f: impl FnOnce(Style)在容器或里面的组件上按住主键时的外观,叠加在 hover 之上。
.focus_within(f: impl FnOnce(Style)里面的控件有键盘焦点时的外观。
.children(f: impl FnOnce(&mut Ui<'a>)放入容器,子组件在 f 里声明;返回 f 的结果。
.children_with_response(f: impl FnOnce(&mut Ui<'a>)和 children 一样,但还返回容器的交互结果。指针在容器或里面的组件上时,容器都算悬停和按下。点在里面的组件上,点击归那个组件。
.w(width: impl Into<Sizing>)设置宽度规则。
.h(height: impl Into<Sizing>)设置高度规则。
.size(width: impl Into<Sizing>, height: impl Into<Sizing>)设置宽度和高度规则。
.w_fill()占满剩余宽度(Sizing::FILL)。
.h_fill()占满剩余高度。
.fill()占满剩余的宽度和高度。
.min_w(size: impl Length)设置最小宽度,优先于宽度规则和内容。
.max_w(size: impl Length)设置最大宽度,文字按它折行。min_w 优先。
.min_h(size: impl Length)设置最小高度,优先于高度规则和内容。
.max_h(size: impl Length)设置最大高度。min_h 优先。
.p(padding: impl Length)设置四边的内边距。
.px(padding: impl Length)设置左右的内边距。
.py(padding: impl Length)设置上下的内边距。
.pt(padding: impl Length)设置上边的内边距。
.pr(padding: impl Length)设置右边的内边距。
.pb(padding: impl Length)设置下边的内边距。
.pl(padding: impl Length)设置左边的内边距。
.padding(padding: Edges)分别设置各边的内边距。
.gap(gap: impl Length)设置子组件之间的间距,也是换行后行与行的间距。
.justify_start()子组件靠起点排列(默认)。
.justify_center()子组件沿布局方向居中。
.justify_end()子组件靠终点排列。
.justify_between()把剩余空间分到子组件之间(Align::Between)。
.justify_around()把剩余空间分到子组件周围(Align::Around)。
.justify_evenly()均匀分配剩余空间(Align::Evenly)。
.items_baseline()让横排里的文字按基线对齐(Align::Baseline)。
.self_start()自身在垂直于排列方向上靠起点对齐(叠放时靠上)。
.self_center()自身在垂直于排列方向上居中(叠放时竖直居中)。
.self_end()自身在垂直于排列方向上靠终点对齐(叠放时靠下)。
.self_stretch()自身在垂直于排列方向上拉伸(叠放时竖直拉伸),前提是该方向的尺寸不固定。
.justify_self_start()叠放时自身靠左,其他情况下忽略。
.justify_self_center()叠放时自身水平居中,其他情况下忽略。
.justify_self_end()叠放时自身靠右,其他情况下忽略。
.aspect_ratio(ratio: f32)设置宽高比。
.gap_x(gap: impl Length)设置横向间距,覆盖 gap。
.gap_y(gap: impl Length)设置纵向间距,覆盖 gap。
.layout_priority(priority: f32)设置一行放不下时保留宽度的优先级。
.col_span(columns: u32)在所在的网格中跨 columns 列。
.offset(x: impl Length, y: impl Length)把元素向右、向下偏移,负值则反向。其他元素不动。
.items_start()子组件在垂直于排列方向上靠起点对齐(竖排的默认值)。
.items_center()子组件在垂直于排列方向上居中(横排的默认值)。
.items_end()子组件在垂直于排列方向上靠终点对齐。
.items_stretch()子组件在垂直于排列方向上拉伸,该方向尺寸固定的除外。
.center()子组件在两个方向上都居中。
.flex_row()横向排列。
.flex_col()纵向排列。
.flex_wrap()一行放满后换行。
.bg(color: Color)设置底色。
.rounded(radius: impl Length)设置圆角半径。超过高度的一半时,两端为半圆。
.border(width: impl Length, color: Color)设置边框,画在边缘以内。
.overflow_hidden()裁掉子组件超出节点范围的部分。
.shadow(blur: impl Length, offset: impl Length, color: Color)设置阴影。
.ring(width: impl Length, color: Color)设置焦点环,画在边框外。
.opacity(opacity: f32)设置子树的不透明度(0–1)。