Xamarin 學習筆記 - Layout(佈局)

本文翻譯自CodeProject文章:https://www.codeproject.com/Articles/1227733/Xamarin-Notes-Xamarin-Forms-Layoutsandroid

轉載請註明出處:葡萄城官網,葡萄城爲開發者提供專業的開發工具、解決方案和服務,賦能開發者。ide

 

在本篇教程中,咱們將瞭解Xamarin.Forms中幾個經常使用的Layout類型並介紹使用這幾種佈局相似進行跨平臺移動開發時的示例。工具

 

StackLayout(棧佈局)

StackLayout容許您將視圖以垂直方向堆疊或以水平方向堆疊,這是最經常使用的佈局。查看文檔以獲取更多詳細信息。佈局

<StackLayout>
        <Label x:Name="MainLable"
               HorizontalOptions="Center"
               FontSize="30"
               TextColor="Black"></Label>
    </StackLayout>
  • Orientation

設置 Horizontal 或者 Vertical。默認值是Vertical。學習

<StackLayout Orientation="Horizontal"> or <StackLayout Orientation="Vertical">
  • LayoutOptions定位

視圖能夠根據相對於佈局的視圖位置設置爲 VerticalOptions 或者 HorizontalOptions ,在這一部分咱們中,咱們將描述如何使用StackLayout面板將視圖組裝到水平或垂直堆疊中。開發工具

<StackLayout Orientation="Horizontal">
           <Button x:Name="Button1" Text="Button1" BackgroundColor="Red"></Button>
           <Button x:Name="Button2" Text="Button2" BackgroundColor="Aqua"></Button>
  </StackLayout>

  <StackLayout Orientation="Vertical">
           <Button x:Name="Button1" Text="Button1" BackgroundColor="Red"></Button>
           <Button x:Name="Button2" Text="Button2" BackgroundColor="Aqua"></Button>
  </StackLayout>

 

在咱們的示例中,咱們將兩個按鈕組合成一個水平堆疊效果(如第一張圖片所示)。ui

VerticalOptions 以及 HorizontalOptions 使用如下值:spa

  • Start:該選項將View放置在佈局的起始位置。
  • End:該選項和Start恰好相反,將View放置在佈局的結束位置。
  • Fill:該選項將View撐滿布局,不留白。
  • Center:該選項將視圖放置在佈局的正中。

視圖是如何在父視圖中對齊的?翻譯

<StackLayout>
        <Label HorizontalOptions="Start" BackgroundColor="Blue" Text="Start"></Label>
         <Label HorizontalOptions="End" BackgroundColor="Red" Text="End"></Label>
         <Label HorizontalOptions="Center" BackgroundColor="Yellow" Text="Center"></Label>
         <Label HorizontalOptions="Fill" BackgroundColor="Green" Text="Fill"></Label>
</StackLayout>

用C#代碼設置以下3d

var stack = new StackLayout();
        var labelStart = new Label()
        {
            HorizontalOptions = LayoutOptions.Start,
            BackgroundColor = Color.Blue,
            Text = "Start"
        };
        var labelEnd = new Label()
        {
          HorizontalOptions = LayoutOptions.End,
          BackgroundColor = Color.Red,
          Text = "End"
        };
        var labelCenter = new Label()
       {
        HorizontalOptions = LayoutOptions.Center,
        BackgroundColor = Color.Yellow,
        Text = "Center"
        };
          var labelFill = new Label()
      {

    HorizontalOptions = LayoutOptions.Fill,

    BackgroundColor = Color.Green,

    Text = "Fill"

};

stack.Children.Add(labelStart);

stack.Children.Add(labelEnd);

stack.Children.Add(labelCenter);

        stack.Children.Add(labelFill);

Content = stack;

<StackLayout Orientation="Vertical">

          <Label HeightRequest="100" BackgroundColor="Blue" Text="One"/>

            <Label HeightRequest="50" BackgroundColor="Red" Text="Two"/>

           <Label HeightRequest="200" BackgroundColor="Yellow" Text="Three"/>

</StackLayout>

 

var stack = new StackLayout()
    {
        Orientation = StackOrientation.Vertical
    };
    var labelOne = new Label()
    {
        HeightRequest = 100,
        BackgroundColor = Color.Blue,
        Text = "One"
    };
    var labelTwo = new Label()
    {
        HeightRequest = 50,
        BackgroundColor = Color.Red,
        Text = "Two"
    };
    var labelThree = new Label()
    {
        HeightRequest = 200,
        BackgroundColor = Color.Yellow,
        Text = "Three"
    };

    stack.Children.Add(labelOne);
    stack.Children.Add(labelTwo);
    stack.Children.Add(labelThree);
    Content = stack;
  • Spacing

能夠設置爲整數或者小數。

<StackLayout Orientation="Vertical" BackgroundColor="Gray">

            <StackLayout Orientation="Horizontal">

                <Label  BackgroundColor="Blue" Text="Start"></Label>

                <Label BackgroundColor="Red" Text="End"></Label>

                <Label BackgroundColor="Yellow" Text="Center"></Label>

            </StackLayout>

            <StackLayout Orientation="Vertical" BackgroundColor="DarkBlue">

                <Button x:Name="Button1" Text="Button1" BackgroundColor="Red"></Button>

                <Button x:Name="Button2" Text="Button2" BackgroundColor="Aqua"></Button>

            </StackLayout>

若是咱們在第一個StackLayout設置了Spacing:

<StackLayout Orientation="Horizontal" Spacing="-6"> or 

<StackLayout Orientation="Horizontal" Spacing="0">

  • Padding 和 Margin

如下是一個示例:

 <StackLayout Orientation="Vertical" BackgroundColor="Gray" >

            <StackLayout Orientation="Horizontal" Spacing="10" Padding="50,20,100,150">

                <Label  BackgroundColor="Blue" Text="Start"></Label>

                <Label BackgroundColor="Red" Text="End"></Label>

                <Label BackgroundColor="Yellow" Text="Center"></Label>

            </StackLayout>

            <StackLayout Orientation="Vertical" BackgroundColor="DarkBlue" Spacing="50">

                <Button x:Name="Button1" Text="Button1" BackgroundColor="Red"></Button>

                <Button x:Name="Button2" Text="Button2" BackgroundColor="Aqua"></Button>

            </StackLayout>

</StackLayout>

AbsoluteLayout(絕對佈局)

AbsoluteLayou容許你在指定的絕對位置放置子元素。

有時,你可能但願更多地控制屏幕上某個對象的位置,好比說,你但願將它們錨定到屏幕的邊緣,或者但願覆蓋住多個元素。

在AbsoluteLayou中,咱們會使用最重要的四個值以及八個設置選項。

四個值是由X、Y、Width、Height組成,經過這四個值能夠爲你的佈局進行定位,它們中的每個均可以被設置爲比例值或絕對值。

能夠是絕對值(以像素爲單位)或者比例值(從0到1)

  • 位置:
    •   X:視圖錨定位置的水平位置。
    •   Y:視圖錨定位置的垂直位置。
  • 尺寸:
    •   Width:定義當前視圖的寬度。
    •   Height:定義當前視圖的高度。

值被指定爲邊界和一個標誌的組合。LayoutBounds是由四個值組成的矩形:x,y,寬度和高度。

設置選項

能夠是絕對值Absolute標誌(以像素爲單位)或者比例值Proportional標誌(從0到1)

  • None:所有的數值是絕對值(數值以像素爲單位)。
  • All:表示佈局邊界的所有數值均表示一個比例值(數值從0到1)。
  • WidthProportional:表示寬度是比例值,而其它的數值以絕對值表示。
  • HeightProportional:表示高度是比例值,而其它的數值以絕對值表示。
  • XProportional:表示X座標值是比例值,而將其它的數值做爲絕對值對待。
  • YProportional:表示Y座標值是比例值,而將其它的數值做爲絕對值對待。
  • PositionProportional:表示X和Y的座標值是比例值,而將表示尺寸的數值做爲絕對值表示。
  • SizeProportional:表示Width和Height的值是比例值,而表示位置的數值是絕對值。

更多詳細內容請參見本連接

結構:

<AbsoluteLayout>

      <BoxView Color="Olive"

               AbsoluteLayout.LayoutBounds="X, Y, Width, Height"

               AbsoluteLayout.LayoutFlags="FlagsValue" />

  </AbsoluteLayout>

Proportional 比例示例 1

<BoxView Color="Blue"

                 AbsoluteLayout.LayoutBounds="0, 0, 0.1, 0.5"

                 AbsoluteLayout.LayoutFlags="All" />

Proportional 比例示例 2

<BoxView Color="Blue"

               AbsoluteLayout.LayoutBounds="0, 0, 1, 0.5"

               AbsoluteLayout.LayoutFlags="All" />

Absolute 絕對值示例 1

<BoxView Color="Blue"

                   AbsoluteLayout.LayoutBounds="0, 75, 250, 410"

                   AbsoluteLayout.LayoutFlags="None" />

RelativeLayout(相對佈局)

 

RelativeLayout使用約束來對子視圖進行佈局。更多詳細信息請參見此連接

與AbsoluteLayout相似,在使用RelativeLayout時,咱們能夠將元素疊加在一塊兒,可是它比AbsoluteLayout更增強大,由於你能夠將相對於另外一個元素的位置或大小的約束應用於一個元素。它提供了與元素位置和大小相關的更多控制。

如下是一個示例:

約束

  • Type:它定義了約束是相對於父仍是另外一個視圖,咱們可使用如下值:RelativeToParent或Constant或RelativeToView。
  • Property:它定義了咱們須要使用哪一個屬性做爲約束的基礎。它的值能夠是Width或Height或者X再或者Y。
  • Factor:被用來應用屬性的值,該值是一個小數,介於0和1之間,能夠寫成0.5e5的格式。
  • Constant:能夠被用做指示一個偏移量的值。
  • ElementName:該約束相對於的視圖的名稱,若是咱們使用關聯到某個視圖的約束關係的話。

 

<ScrollView>
            <RelativeLayout>
 
                <BoxView Color="Gray" HeightRequest="200" 

                    RelativeLayout.WidthConstraint="{ConstraintExpression 
                    Type=RelativeToParent,
                    Property=Width,
                    Factor=1}" />
 
                <Button BorderRadius="35" 

                        x:Name="ImageCircleBack" 

                        BackgroundColor="Blue" 

                        HeightRequest="70" WidthRequest="70" 

                        RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=.5, Constant = -35}" 

                        RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Factor=0, Property=Y, Constant=70}" />
             <Label Text="Hamida REBAÏ" FontAttributes="Bold" FontSize="26" HorizontalTextAlignment="Center" 

                       RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Y, Factor=0, Constant=140}" 

                       RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1}" />
            </RelativeLayout>
        </ScrollView>

咱們能夠獲得如下結果:

 

Grid(網格佈局)

Grid和一個表格同樣。它比StackLayout更加通用,提供列和行兩個維度以供輔助定位。在不一樣行之間對齊視圖也很容易。實際使用起來與WPF的Grid很是相似甚至說沒什麼區別。

在這一部分,咱們將學習如何建立一個Grid並指定行和列。

 

<Grid>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
 
            <Button Text="7" Grid.Row="1" Grid.Column="0"

       BackgroundColor="White" TextColor="Black"

       FontSize="36" BorderRadius="0" />
            <Button Text="8" Grid.Row="1" Grid.Column="1"

       BackgroundColor="White" TextColor="Black"

       FontSize="36" BorderRadius="0" />
            <Button Text="9" Grid.Row="1" Grid.Column="2"

       BackgroundColor="White" TextColor="Black"

       FontSize="36" BorderRadius="0" />
 
            <Button Text="4" Grid.Row="2" Grid.Column="0"

       BackgroundColor="White" TextColor="Black"

       FontSize="36" BorderRadius="0" />
            <Button Text="5" Grid.Row="2" Grid.Column="1"

       BackgroundColor="White" TextColor="Black"

       FontSize="36" BorderRadius="0" />
            <Button Text="6" Grid.Row="2" Grid.Column="2"

       BackgroundColor="White" TextColor="Black"

       FontSize="36" BorderRadius="0" />
 
            <Button Text="1" Grid.Row="3" Grid.Column="0"

       BackgroundColor="White" TextColor="Black"

       FontSize="36" BorderRadius="0" />
            <Button Text="2" Grid.Row="3" Grid.Column="1"

       BackgroundColor="White" TextColor="Black"

       FontSize="36" BorderRadius="0" />
            <Button Text="3" Grid.Row="3" Grid.Column="2"

       BackgroundColor="White" TextColor="Black"

       FontSize="36" BorderRadius="0" />
            <Button Text="0" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="3"

       BackgroundColor="White" TextColor="Black"

       FontSize="36" BorderRadius="0" />
 
            <Button Text="/" Grid.Row="1" Grid.Column="3"

        BackgroundColor="#FFA500" TextColor="White"

        FontSize="36" BorderRadius="0" />
            <Button Text="X" Grid.Row="2" Grid.Column="3"

        BackgroundColor="#0000ff" TextColor="White"

        FontSize="36" BorderRadius="0" />
            <Button Text="-" Grid.Row="3" Grid.Column="3"

        BackgroundColor="#8000ff" TextColor="White"

        FontSize="36" BorderRadius="0" />
            <Button Text="+" Grid.Row="4" Grid.Column="3"

        BackgroundColor="#0080ff" TextColor="White"

        FontSize="36" BorderRadius="0" />
 
            <Button Text="C" Grid.Row="5" Grid.Column="0"

        BackgroundColor="#808080" TextColor="White"

        FontSize="36" BorderRadius="0" />
 
            <Button Text="=" Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="3"

        BackgroundColor="#000066" TextColor="White"

        FontSize="36" BorderRadius="0" />
 
</Grid>

咱們首先在Grid中使用這些標記定義行數和列數。

<Grid.RowDefinitions>

     <RowDefinition/>

     <RowDefinition/>

     <RowDefinition/>

     <RowDefinition/>

     <RowDefinition/>

     <RowDefinition/>

</Grid.RowDefinitions>

<Grid.ColumnDefinitions>

     <ColumnDefinition/>

     <ColumnDefinition/>

     <ColumnDefinition/>

     <ColumnDefinition/>

</Grid.ColumnDefinitions>

 在此以後,咱們將在其中排布視圖

 例如:

<Button Text="7" Grid.Row="1" Grid.Column="0" BackgroundColor="White" TextColor="Black" FontSize="36" BorderRadius="0" />

該按鈕將被放置在第二行(Grid.Row="1")第一列(Grid.Column="0")。

使用Height屬性定義行的高度:

<Grid.RowDefinitions>

  <RowDefinition Height="Auto" />

該值能夠是Auto或者100或者星號(*),咱們能夠指定2*(甚至n*)。

使用Width屬性定義列的寬度:

<Grid.ColumnDefinitions>

                <ColumnDefinition Width="*"/>

該值能夠是Auto或者100或者星號(*),咱們能夠指定2*(甚至n*)。

 

<Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="100" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="2*"/>
            </Grid.ColumnDefinitions>
            <Label Grid.Column="0" Grid.Row="0" Text="1" BackgroundColor="Red" ></Label>
            <Label Grid.Column="1" Grid.Row="0" Text="2" BackgroundColor="Red"></Label>
            <Label Grid.Column="0" Grid.Row="1" Text="3" BackgroundColor="Red"></Label>
            <Label Grid.Column="1" Grid.Row="1" Text="4" BackgroundColor="Red"></Label>
            <Label Grid.Column="0" Grid.Row="2" Text="5" BackgroundColor="Red"></Label>
            <Label Grid.Column="1" Grid.Row="2" Text="6" BackgroundColor="Red"></Label>
        </Grid>

ScrollView

ScrollView是一個能夠滾動的內容。

若是不使用ScrollView:

<StackLayout>
            <BoxView Color="Blue" HeightRequest="200"/>
            <BoxView Color="Green" HeightRequest="200"/>
            <BoxView Color="Firebrick" HeightRequest="200"/>
            <BoxView Color="YellowGreen" HeightRequest="300"/>
</StackLayout>

在以上示例中,顏色爲Yellow Green的BoxView將不顯示,而後咱們向其中添加一個ScrollView,經過滾動,咱們就能夠看到所有的內容。ScrollView將向界面UI添加一個滾動指示器。當咱們須要指定水平滾動或者垂直滾動,再或者雙向滾動時,咱們可使用到Orientation屬性。

 

<ScrollView Orientation="Horizontal"> or <ScrollView Orientation="Vertical"> or <ScrollView Orientation="Both">

<ScrollView>

            <StackLayout>

                <BoxView Color="Blue" HeightRequest="200"/>

                <BoxView Color="Green" HeightRequest="200"/>

                <BoxView Color="Firebrick" HeightRequest="200"/>

                <BoxView Color="YellowGreen" HeightRequest="300"/>

</StackLayout>

</ScrollView>

更多詳細信息,請參見此連接

ScrollView一般被用來顯示一個列表(ListView)。

下篇文章咱們將說一說Page(頁面)相關的內容。

相關文章
相關標籤/搜索