2.1 布局之StackLayout(栈布局)

1.介绍

1.1 基础

对于了解iOS9新特性的人来说,这个很好理解,相当于iOS里面的UIStackView,你可以把它理解成一个矩形容器,强调一下是容器,不是视图,就相当于是个纸箱子,纸箱子里面放了一堆控件,然后各个纸箱子之间水平摆放。画个图理解一下



画的有点丑,除了蓝色其他的都是StackLayout,如果界面复杂点就是StackLayout里套StackLayout。StackLayout的特性就是垂直或者水平排放,就拿红色的StackLayout来说,里面的圆角矩形和一个小长条不能用StackLayout,但是我们可以把两个小长条放在一个StackLayout里面垂直排布,然后和外面的圆角矩形放在一个StackLayout里面水平排布,还是看图吧。


这样应该好理解了。重点是多写代码,写多了自然对它就有感觉了。

1.2 重点属性: VerticalOptions和HorizontalOptions

  • 他们分别是垂直约束条件和水平约束条件

  • 重点是它有哪些值:

  • Start:放置在layout起始位置

  • End:放置在layout最后位置

  • Fill:放置在layout中使其没有间距

  • StartAndExpand:放置在layout起始位置,并尽可能占据layout所能给的最大空间

  • CenterAndExpand:放置在layout中间位置,并尽可能占据layout所能给的最大空间

  • EndAndExpand:放置在layout最后位置,并尽可能占据layout所能给的最大空间

  • FillAndExpand:放置在layout中使其没有间距,并尽可能占据layout所能给的最大空间

2. XAML示例代码

<ContentPage.Content>
        <StackLayout Padding = "5,10" x:Name="layout">
            <Label TextColor = "#77d065" Text="This is a green label." BackgroundColor = "Red"/>
            <Label Text = "This is a default, non-customized label." BackgroundColor = "Red" />
            <Label FontSize = "30" Text="This label has a font size of 30." BackgroundColor = "Red" />
            <Label FontAttributes = "Bold" Text="This is bold text." BackgroundColor = "Red" />
            <Label BackgroundColor = "Green" >
                < Label.FormattedText >
                    < FormattedString >
                        < Span Text="Red Bold" ForegroundColor="Red" FontAttributes="Bold" />
                        <Span Text = "Default" />
                        < Span Text="italic small" FontAttributes="Italic" FontSize="Small" />
                    </FormattedString>
                </Label.FormattedText>
            </Label>
        </StackLayout>
</ContentPage.Content>  

3. 效果图

Paste_Image.png

4. StackLayout官方介绍链接

5. 其他示例

<StackLayout 
    Padding = "5, 5, 0, 5"
    Orientation="Horizontal" 
    Spacing="15"
    Margin="0,0,0,10"> 

5.1. Padding属性的值 , 内边距

  • 顺序: 左 上 右 下
  • 例如: 上面这个StackLayout的间距设置的是左 上 下三个方向的内部边距
  • Padding如果有两个值,第一个表示水平的左和右 , 第二个表示竖直的上和下

5.2 Spacing的值表示内部的item之间的间距

5.3 Orientation表示StackLayout的布局方向(水平和竖直两个方向)是水平方向

5.4 Margin表示StackLayout相对于父层级的边距(左上右下的顺序)https://developer.xamarin.com/samples/xamarin-forms/all/

6.6 本文部分内容转载自这里:http://www.jianshu.com/p/de2025c17cd8

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容