xamarin forms常用的布局stacklayout詳解

來源:互聯網
上載者:User

標籤:namespace   位置   add   ice   enter   margin   高度寬度   實現   back   

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

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

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

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使用的最為廣泛和簡單,其他的幾種布局寫起來相對複雜,效果也相對不佳。

xamarin forms常用的布局stacklayout詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.