Xamarin.Form 的 LayoutOptions 有什么区别,尤其是 Fill 和 Expand? [英] What is the difference between Xamarin.Form's LayoutOptions, especially Fill and Expand?

查看:33
本文介绍了Xamarin.Form 的 LayoutOptions 有什么区别,尤其是 Fill 和 Expand?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Xamarin.Forms 中,每个 View 都有两个属性 Horizo​​ntalOptionsVerticalOptions.两者都是 LayoutOptions 类型,并且可以具有以下值之一:

In Xamarin.Forms every View has the two properties HorizontalOptions and VerticalOptions. Both are of type LayoutOptions and can have one of the following values:

  • LayoutOptions.Start
  • LayoutOptions.Center
  • LayoutOptions.End
  • LayoutOptions.Fill
  • LayoutOptions.StartAndExpand
  • LayoutOptions.CenterAndExpand
  • LayoutOptions.EndAndExpand
  • LayoutOptions.FillAndExpand

显然它控制视图在父视图上的对齐方式.但是每个选项的行为究竟如何?而Fill和后缀Expand有什么区别?

Apparently it controls the view's alignment on the parent view. But how exactly is the behavior of each individual option? And what is the difference between Fill and the suffix Expand?

推荐答案

简短回答

StartCenterEndFill 定义了视图在其空间内的对齐方式.

Short answer

Start, Center, End and Fill define the view's alignment within its space.

Expand 定义是否占用更多空间(如果可用).

结构 LayoutOptions 控制两种不同的行为:

The structure LayoutOptions controls two distinct behaviors:

  1. 对齐: 视图在父视图中如何对齐?

  • Start:为了垂直对齐,视图移到顶部.对于水平对齐,这通常是左侧.(但请注意,在语言设置为从右到左的设备上,这是相反的,即右对齐.)
  • Center:视图居中.
  • End:通常视图底部或右对齐.(当然,在从右到左的语言中,左对齐.)
  • Fill:这种对齐方式略有不同.该视图将横跨父视图的整个尺寸.
  • Start: For vertical alignment the view is moved to the top. For horizontal alignment this is usually the left-hand side. (But note, that on devices with right-to-left language setting this is the other way around, i.e. right aligned.)
  • Center: The view is centered.
  • End: Usually the view is bottom or right aligned. (On right-to-left languages, of course, left aligned.)
  • Fill: This alignment is slightly different. The view will stretch across the full size of the parent view.

但是,如果父级不大于其子​​级,您将不会注意到这些对齐方式之间的任何差异.对齐仅对具有额外可用空间的父视图很重要.

If the parent, however, is not larger then its children, you won't notice any difference between those alignments. Alignment only matters for parent views with additional space available.

扩展: 如果可用,元素会占用更多空间吗?

  • 后缀Expand:如果父视图大于其所有子视图的总和,即有额外的空间可用,则空间在具有该后缀的子视图之间成比例.这些孩子会占据"他们的空间,但不一定填满"它.我们将在下面的示例中查看此行为.
  • 无后缀:没有 Expand 后缀的子级不会获得额外空间,即使有更多空间可用.
  • Suffix Expand: If the parent view is larger than the combined size of all its children, i.e. additional space is available, then the space is proportioned amongst child views with that suffix. Those children will "occupy" their space, but do not necessarily "fill" it. We'll have a look on this behavior in the example below.
  • No suffix: The children without Expand suffix won't get additional space, even if more space is available.

同样,如果父视图不大于其子​​视图,则扩展后缀也没有任何区别.

Again, if the parent view is not larger than its children, the expansion suffix does not make any difference as well.

示例

让我们看一下下面的例子,看看所有八个布局选项之间的区别.

Example

Let's have a look on the following example to see the difference between all eight layout options.

该应用程序包含一个带有八个嵌套白色按钮的深灰色 StackLayout,每个按钮都标有其垂直布局选项.单击其中一个按钮时,它会将其垂直布局选项分配给堆栈布局.通过这种方式,我们可以轻松地测试视图与父级的交互,两者都具有不同的布局选项.

The app contains a dark gray StackLayout with eight nested white buttons, each of which is labeled with its vertical layout option. When clicking on one of the buttons, it assignes its vertical layout option to the stack layout. This way we can easily test the interaction of views with parents, both with different layout option.

(最后几行代码添加了额外的黄色框.我们稍后会回到这一点.)

(The last few lines of code add additional yellow boxes. We'll come back to this in a moment.)

public static class App
{
    static readonly StackLayout stackLayout = new StackLayout {
        BackgroundColor = Color.Gray,
        VerticalOptions = LayoutOptions.Start,
        Spacing = 2,
        Padding = 2,
    };

    public static Page GetMainPage()
    {
        AddButton("Start", LayoutOptions.Start);
        AddButton("Center", LayoutOptions.Center);
        AddButton("End", LayoutOptions.End);
        AddButton("Fill", LayoutOptions.Fill);
        AddButton("StartAndExpand", LayoutOptions.StartAndExpand);
        AddButton("CenterAndExpand", LayoutOptions.CenterAndExpand);
        AddButton("EndAndExpand", LayoutOptions.EndAndExpand);
        AddButton("FillAndExpand", LayoutOptions.FillAndExpand);

        return new NavigationPage(new ContentPage {
            Content = stackLayout,
        });
    }

    static void AddButton(string text, LayoutOptions verticalOptions)
    {
        stackLayout.Children.Add(new Button {
            Text = text,
            BackgroundColor = Color.White,
            VerticalOptions = verticalOptions,
            HeightRequest = 20,
            Command = new Command(() => {
                stackLayout.VerticalOptions = verticalOptions;
                (stackLayout.ParentView as Page).Title = "StackLayout: " + text;
            }),
        });
        stackLayout.Children.Add(new BoxView {
            HeightRequest = 1,
            Color = Color.Yellow,
        });
    }
}

以下屏幕截图显示了单击八个按钮中的每一个时的结果.我们做出以下观察:

The following screenshots show the result when clicking on each of the eight buttons. We make the following observations:

  • 只要父stackLayout紧(不Fill页面),每个Button的垂直布局选项可以忽略不计.
  • 垂直布局选项仅在 stackLayout 较大(例如通过 Fill 对齐)并且各个按钮具有 Expand 后缀时才重要.
  • 附加空间在所有带有 Expand 后缀的按钮之间按比例分配.为了更清楚地看到这一点,我们在每两个相邻按钮之间添加了黄色水平线.
  • 空间大于其请求高度的按钮不一定填充"它.在这种情况下,实际行为由它们的对齐控制.例如.它们要么在空间的顶部、中心或按钮上对齐,要么完全填充.
  • 所有按钮跨越整个布局宽度,因为我们只修改了VerticalOptions.
  • As long as the parent stackLayout is tight (does not Fill the page), the vertical layout option of each Button is negligible.
  • The vertical layout option only matters if the stackLayout is larger (e.g. via Fill alignment) and the individual buttons have the Expand suffix.
  • Additional space is evently proportioned amongst all buttons with Expand suffix. To see this more clearly we added yellow horizontal lines between every two neighboring buttons.
  • Buttons with more space than their requested height do not necessarily "fill" it. In this case the actual behavior is controlled by their alignment. E.g. they are either aligned on top, center or button of their space or fill it completely.
  • All buttons span across the whole width of the layout, since we only modify the VerticalOptions.

您可以在此处找到相应的高分辨率屏幕截图.

这篇关于Xamarin.Form 的 LayoutOptions 有什么区别,尤其是 Fill 和 Expand?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆