xamarin forms常用的布局StackLayout詳解


通過這篇文章你將了解到xamarin forms中最簡單常用的布局StackLayout。至於其他幾種布局使用起來,效果相對較差,目前在項目中使用最多的也就是這兩種布局StackLayout和Grid。

之前上一家的的同事在寫xamarin android的時候,聊天給我說他寫axml布局的時候都是拖控件,這有點刷新我認知的下線,一直拖控件“歷史原因”,造成的壞處是顯而易見的,無法熟練掌握布局的常用屬性,至於xamarin forms能不能拖控件,就目前來說是不能的,布局的設計有兩種實現方式,一種是以c#代碼的方式,一種是以xaml布局的方式。

如下圖是xamarin forms中最見的五種布局,本篇文章將使用最常用的一種布局StackLayout,實現一個簡易計算器的布局,便於熟悉和掌握這種布局的各種屬性。

xamarin forms常用的布局stacklayout、grid詳解

StackLayout相似於android中LinearLayout、前端css中的默認的Static定位;Grid相似於android中GridLayout,html中的Table布局。

1.StackLayout布局屬性和屬性值的作用

顧名思義,StackLayout是一種可以在上下方向、左右方向堆疊的布局,簡單而又常用的布局,我們需要掌握它的三個重要屬性,最重要的是布局方向和布局定位。

  • Orientation :布局方向,枚舉類型,表示StackLayout以哪種方向的布局, Vertical (垂直方向布局) 和
    Horizontal(水平方向布局),默認值是Vertical.
  • Spacing :double類型,表示每個子視圖之間的間隙, 默認值 6.0.
  • VerticalOptions和HorizontalOptions:布局定位(既可以定位又可以設置布局元素大小),該屬性的屬性值有8個分別是
    1. Start:在父布局開始位置
    2. Center:在父布局中間位置
    3. End:在父布局最后位置
    4. Fill:填充整個父布局的位置
    5. StartAndExpand、CenterAndExpand、EndAndExpand、FillAndExpand,這種帶AndExpand的作用就是:根據其他布局的內容大小,如果有空白位置就會自動填充。當多個屬性值都是AndExpand則會平分空白部分。
      直接來個布局看看這些個屬性到底是怎么用的吧
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:XamarinFormsLayout"
             x:Class="XamarinFormsLayout.MainPage">
    <StackLayout Orientation="Vertical">
        <StackLayout Orientation="Vertical" BackgroundColor="Accent" VerticalOptions="FillAndExpand" Padding="10">
            <Label Text="我在左邊" 
           HeightRequest="100"
           WidthRequest="200"
           HorizontalOptions="Start"
           VerticalOptions="Start"
           BackgroundColor="AliceBlue"
           TextColor="Black"
           VerticalTextAlignment="Center"/>
            <Label Text="我在右邊" 
           HorizontalOptions="End"
           VerticalOptions="End"
           BackgroundColor="AliceBlue"
           TextColor="Black"
           VerticalTextAlignment="Center"/>
        </StackLayout>
        <StackLayout Orientation="Horizontal" BackgroundColor="Aquamarine" VerticalOptions="Start" HeightRequest="50">
            <Label HorizontalOptions="Start" VerticalOptions="CenterAndExpand"  Text="我在左邊" TextColor="Black" BackgroundColor="Azure"></Label>
            <Label HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand"  Text="占滿中間位置" TextColor="Black" BackgroundColor="Azure"></Label>
            <Label HorizontalOptions="End" VerticalOptions="CenterAndExpand"  Text="我在右邊" TextColor="Black" BackgroundColor="Azure"></Label>
        </StackLayout>
        <StackLayout Orientation="Vertical" BackgroundColor="Accent"  Padding="10"  VerticalOptions="FillAndExpand">
            <!-- Place new controls here -->
            <Label Text="我在頂部,高度平分" 
              HorizontalOptions="StartAndExpand"
              VerticalOptions="FillAndExpand"
              BackgroundColor="Red"/>
            <Label Text="我在中間,高度平分" 
              HorizontalOptions="FillAndExpand"
              VerticalOptions="FillAndExpand"
              BackgroundColor="Red"/>
            <Label Text="我在底部" 
              HorizontalOptions="FillAndExpand"
              VerticalOptions="EndAndExpand"
              BackgroundColor="Red"/>
        </StackLayout>
    </StackLayout>
</ContentPage>

直接設置高度寬度可以用HeightRequest和WidthRequest;

2.StackLayout布局重點需要掌握

2.1 VerticalOptions和HorizontalOptions與WidthRequest和HeightRequest的優先級關系是什么?

這一點容易混淆,我們已經知道VerticalOptions和HorizontalOptions是用來定位和設置大小的,WidthRequest和HeightRequest是double類型,只能用來設置控件大小。當都設置了這四個屬性,會出現什么樣的結果。

里面兩個子StackLayout的高度各占50%,我們發現** Options和**Request 的屬性值所定義的大小誰大就以誰的值為主。

2.2 在垂直方向(水平方向)設置寬度WidthRequest(高度HeightRequest)無效,如圖:

3.StackLayout實現一個簡易的計算器布局

代碼如下:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="XamarinFormsLayout.CalculatorPage"
             BackgroundColor="#808080">
    <ContentPage.Resources>
        <ResourceDictionary>
            <Style x:Key="DefaultButton" TargetType="Button">
                <Setter Property="BackgroundColor" Value="Black"></Setter>
                <Setter Property="TextColor" Value="#dedede"></Setter>
            </Style>
        </ResourceDictionary>
    </ContentPage.Resources>
    <StackLayout Orientation="Vertical"  Spacing="10" VerticalOptions="End" Padding="10">
        <Frame BackgroundColor="White" HeightRequest="40" Margin="0,0,0,20">
            <Label Text="0" VerticalOptions="Center" HorizontalOptions="End"TextColor="Black"FontSize="35"/>
        </Frame>
        <StackLayout Orientation="Vertical">
            <StackLayout Orientation="Horizontal"   Spacing="10">
                <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand">
                    <Button  Text="清除" HeightRequest="60" Style="{StaticResource DefaultButton}"/>
                    <StackLayout Orientation="Horizontal" HeightRequest="60">
                        <Button HorizontalOptions="FillAndExpand"   Text="7"  Style="{StaticResource DefaultButton}"/>
                        <Button HorizontalOptions="FillAndExpand"  Text="8" Style="{StaticResource DefaultButton}" />
                        <Button HorizontalOptions="FillAndExpand"  Text="9" Style="{StaticResource DefaultButton}" />
                    </StackLayout>
                    <StackLayout Orientation="Horizontal" HeightRequest="60">
                        <Button HorizontalOptions="FillAndExpand"  Text="4" Style="{StaticResource DefaultButton}" />
                        <Button HorizontalOptions="FillAndExpand"  Text="5" Style="{StaticResource DefaultButton}"/>
                        <Button HorizontalOptions="FillAndExpand"  Text="6" Style="{StaticResource DefaultButton}"/>
                    </StackLayout>
                    <StackLayout Orientation="Horizontal" HeightRequest="60">
                        <Button HorizontalOptions="FillAndExpand"   Text="1" Style="{StaticResource DefaultButton}" />
                        <Button HorizontalOptions="FillAndExpand"  Text="2" Style="{StaticResource DefaultButton}"/>
                        <Button HorizontalOptions="FillAndExpand"   Text="3" Style="{StaticResource DefaultButton}"/>
                    </StackLayout>
                    <StackLayout Orientation="Horizontal" HeightRequest="60">
                        <Button HorizontalOptions="FillAndExpand"  Text="0" Style="{StaticResource DefaultButton}"/>
                        <Button HorizontalOptions="FillAndExpand"  Text="." Style="{StaticResource DefaultButton}"/>
                    </StackLayout>
                </StackLayout>
                <StackLayout Orientation="Vertical" WidthRequest="60">
                    <Button  Text="÷"  HeightRequest="60" Style="{StaticResource DefaultButton}"/>
                    <Button Text="*" HeightRequest="60" Style="{StaticResource DefaultButton}"/>
                    <Button Text="+" HeightRequest="60" Style="{StaticResource DefaultButton}"/>
                    <Button Text="-" HeightRequest="60" Style="{StaticResource DefaultButton}"/>
                    <Button Text="=" HeightRequest="60" Style="{StaticResource DefaultButton}"/>
                </StackLayout>
            </StackLayout>
        </StackLayout>
    </StackLayout>
</ContentPage>

4.總結

xamarin forms的布局都是基於wpf的思想,padding和margin的四個方向是左上右下,這和android、前端css的四個方向上右下左有點區別。
常用的布局就我個人而言StackLayout和Grid使用的最為廣泛和簡單,其他的幾種布局寫起來相對復雜,效果也相對不佳。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM