Main Content

uitoolbar

在图窗中创建工具栏

说明

tb = uitoolbar 在当前图窗中创建一个工具栏并返回 Toolbar 对象。如果尚不存在用 figure 函数创建的图窗,则 MATLAB® 会创建一个图窗作为父级。

示例

tb = uitoolbar(parent) 在指定的父图窗中创建一个工具栏。父容器可以是使用 uifigurefigure 函数创建的图窗。

tb = uitoolbar(___,Name,Value) 创建一个工具栏,其中包含使用一个或多个名称-值对组参数指定的属性值。用上述任一语法指定名称-值对组。

示例

全部折叠

通过调用 figure 函数来创建一个图窗。向图窗添加一个工具栏。它出现在默认图窗工具栏下方。

f = figure;
tb = uitoolbar(f);

Figure that displays the default toolbar and another empty toolbar below it.

在工具栏中创建一个按钮工具。从索引图像文件 matlabicon.gif 中读取一个新图标。将索引图像转换为 RGB 真彩色图像数组。通过将 CData 属性设置为真彩色图像数组,将图标添加到按钮工具。

pt = uipushtool(tb);

[img,map] = imread(fullfile(matlabroot,...
            'toolbox','matlab','icons','matlabicon.gif'));
ptImage = ind2rgb(img,map);

pt.CData = ptImage;

Figure that displays the default toolbar and another toolbar below it that contains a push tool with the MathWorks logo.

更改工具栏中从左到右的工具顺序。在本例中,颠倒 UI 图窗工具栏中按钮工具和切换工具的顺序。

创建一个 UI 图窗。向其中添加一个工具栏。然后,在工具栏中添加按钮工具和切换工具。

fig = uifigure;
tb = uitoolbar(fig);
pt = uipushtool(tb);
tt = uitoggletool(tb);

UI figure with a push tool and toggle tool in a toolbar that both display the default icon.

在工具栏中创建一个按钮工具。将 Icon 属性值设置为图像文件 matlabicon.gif

pt.Icon = fullfile(matlabroot,'toolbox','matlab','icons','matlabicon.gif');

Membrane logo added to the push tool. The push tool is to the left of the toggle tool.

创建一个蓝色真彩色图像数组。将 Icon 属性值设置为此数组,以在切换工具中显示蓝色方形图标。

ttImage = zeros(16,16,3);
ttImage(:,:,3) = ones(16);
tt.Icon = ttImage;

Blue square added to the toggle tool.

查询工具栏的 Children 属性。在此数组中返回的子级的顺序反映了工具在工具栏中从右到左的显示顺序。切换工具是最右边的工具,出现在列表的顶部(数组的第一个元素)。

oldToolOrder = tb.Children
oldToolOrder = 

  2×1 graphics array:

  ToggleTool
  PushTool

通过调用 flipud 函数翻转在 tb.Children 返回的数组中元素的顺序,以颠倒工具的顺序。将 Children 属性值设置为此新工具顺序。按钮工具现在出现在工具栏中切换工具的右侧。

newToolOrder = flipud(oldToolOrder);
tb.Children = newToolOrder;

Blue toggle tool appears to the left of the push tool in the toolbar.

输入参数

全部折叠

父图窗,指定为使用 uifigurefigure 函数创建的 Figure 对象。如果未指定父图窗,则 MATLAB 调用 figure 函数以创建一个图窗来用作父图窗。

名称-值参数

将可选的参数对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参数名称,Value 是对应的值。名称-值参数必须出现在其他参数之后,但参数对组的顺序无关紧要。

在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。

示例: 'Visible','off' 将工具栏的可见性设置为 'off'

注意

此处所列的属性只是一部分。有关完整列表,请参阅 Toolbar 属性

可见性状态,指定为 'on''off',或者指定为数值或逻辑值 1 (true) 或 0 (false)。值 'on' 等效于 true'off' 等效于 false。因此,您可以使用此属性的值作为逻辑值。该值存储为 matlab.lang.OnOffSwitchState 类型的 on/off 逻辑值。

  • 'on' - 显示对象。

  • 'off' - 隐藏对象而不删除它。您仍然可以访问不可见 UI 组件的属性。

要使您的 App 更快地启动,请将不需要在启动时出现的所有 UI 组件的 Visible 属性设置为 'off'

Toolbar 的子级,以空 GraphicsPlaceholder 或组件对象的一维数组形式返回。Toolbar 对象的子级是 PushToolToggleTool 对象。

不能使用 Children 属性添加或删除子级。使用该属性查看子级列表或对子级重新排序。此数组中子级的顺序反映了工具在工具栏中从右到左的显示顺序。这意味着最右边的工具在列表的顶部,最左边的工具在列表的底部。例如,由 Children 属性返回的此工具顺序表示按钮工具出现在工具栏中切换工具的左侧。

toolOrder = tb.Children
toolOrder = 

  2×1 graphics array:

  ToggleTool
  PushTool

要向该列表中添加子级,请将子组件的 Parent 属性设置为 uitoolbar 对象。

HandleVisibility 属性设置为 'off' 的对象不会列在 Children 属性中。

提示

  • 工具栏可以包含按钮工具或切换工具。按钮工具的行为就像普通按钮。当您点击它们时,它们显示为已按下,直到您松开鼠标按键。切换工具有两种状态:'off''on'。每次点击按钮时,按钮的状态都会发生变化。

  • WindowStyle 属性设置为 'modal' 的图窗中,Toolbar 对象(及其子级 PushToolToggleTool 对象)不显示。如果一个含有工具栏子对象的图窗的 WindowStyle 更改为 'modal',则该工具栏子对象仍然存在于图窗的 Children 属性中。但是,在 WindowStyle 设置为 'modal' 时,工具栏不会出现。

版本历史记录

在 R2006a 之前推出