Xamarin Stacklayout VerticalOptions CenterAndExpand无法正常工作

时间:2016-06-01 10:32:16

标签: xamarin xamarin.forms

嗨大家可能非常容易的问题Xamarin专家我试图垂直居中对齐stacklayout控件,它在Android手机中工作正常,但在Windows移动设备中无法正常工作。我的代码在

之下
    <?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="aa.Views.MainPage">
  <ScrollView>
    <StackLayout Orientation="Vertical" VerticalOptions="CenterAndExpand"
                  Padding="20" Spacing="10">
      <Entry x:Name="enEmail" Placeholder="Email"></Entry>
      <Entry x:Name="enPassword" IsPassword="True" Placeholder="Password"></Entry>
      <Button Text="Login" Clicked="OnClicked_btnLogin" x:Name="btnLogin"></Button>
      <Button Text="Register" Clicked="OnClicked_btnRegister" x:Name="btnRegister"></Button>
    </StackLayout>
  </ScrollView>

</ContentPage>

有关详细信息,请查看附加的图像,并帮助我如何在Windows手机的屏幕中心对齐

enter image description here

2 个答案:

答案 0 :(得分:1)

StackLayout:将视图相互连续放置。如果您想将按钮移到下方,请使用Grid代替:

  <Grid RowSpacing="10"><!--RowSpacing gives some space between rows-->
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto"/> <!--You can also use constant size also-->
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="*"/> <!--This fill the empty space-->
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <Entry Grid.Row="0" Placeholder=" Email"/>
    <Entry Grid.Row="1" Placeholder=" Password" IsPassword="True"/>

    <Button Grid.Row="3" Text="Login" />
    <Button Grid.Row="4" Text="Register" />

  </Grid>

答案 1 :(得分:1)

尝试下面的代码,它会给你一个滚动视图,你的字段将与中心对齐

<ScrollView>
      <AbsoluteLayout>
        <StackLayout Orientation="Vertical" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All" x:Name="maincontent" Spacing="0">
          <StackLayout Orientation="Vertical" VerticalOptions="CenterAndExpand"
                  Padding="20" Spacing="10">
            <Entry x:Name="enEmail" Placeholder="Email"></Entry>
            <Entry x:Name="enPassword" IsPassword="True" Placeholder="Password"></Entry>
            <Button Text="Login" Clicked="OnClicked_btnLogin" x:Name="btnLogin"></Button>
            <Button Text="Register" Clicked="OnClicked_btnRegister" x:Name="btnRegister"></Button>
          </StackLayout>
        </StackLayout>
      </AbsoluteLayout>
    </ScrollView>