如何在Xamarin.Forms中设置控件的宽度

时间:2014-09-04 10:40:09

标签: ios ipad xamarin xamarin.forms

我试图调整宽度和宽度我的文本框的高度[入口]控件在Xamarin.Forms for iPad app中,但它没有使用WidthRequest属性设置宽度。

任何人都可以帮我解决一下如何设置控件的宽度。

这是我的XAML代码,使用Grid& StackLayout但它们都没有工作。

<Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" >
    <Grid.RowDefinitions>
    <RowDefinition Height="Auto"  ></RowDefinition>
    <RowDefinition Height="*"></RowDefinition>
    <RowDefinition Height="10"></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*"></ColumnDefinition>
    </Grid.ColumnDefinitions>

    <Image Grid.Row="0" Grid.Column="0" Source="loginlogo.png"></Image>
    <StackLayout Grid.Row="1" Grid.Column="0">
    <Entry Placeholder="Enter Username" WidthRequest="20"></Entry>
    <Entry Placeholder="Enter Password" WidthRequest="20"></Entry>
    <Button Text="Button 1"></Button>
    </StackLayout>
    </Grid>

还尝试使用Stack Layout

<StackLayout Spacing="10">
    <Entry x:Name="txtUserName" Placeholder="Enter Username" WidthRequest="2" HeightRequest="200"></Entry>
    <Entry x:Name="txtPassword" Placeholder="Enter Password" WidthRequest="2" HeightRequest="200"></Entry>
    <Button Text="Button 1"></Button></StackLayout>

2 个答案:

答案 0 :(得分:30)

请注意,您必须使用 WidthRequest 指定 Horizo​​ntalOptions 才能使其在 StackLayout 上运行,因为它会尝试自动展开默认情况下。

StackLayout 下面显示的示例: -

        StackLayout objStackLayout = new StackLayout()
        {
            Spacing = 10
        };
        //
        Entry objEntry1 = new Entry()
        {
            Placeholder = "Enter Username",
            WidthRequest = 300,
            HeightRequest = 200,
            HorizontalOptions = LayoutOptions.Start
        };
        objStackLayout.Children.Add(objEntry1);
        //
        Entry objEntry2 = new Entry()
        {
            Placeholder = "Enter Password",
            WidthRequest = 200,
            HeightRequest = 200,
            HorizontalOptions = LayoutOptions.Start
        };
        objStackLayout.Children.Add(objEntry2);
        //
        Button objButton1 = new Button()
        {
            Text = "Button1",
            HorizontalOptions = LayoutOptions.Start
        };
        objStackLayout.Children.Add(objButton1);

答案 1 :(得分:9)

使用StackLayout组合间距,水平/垂直选项和填充。例如:

<StackLayout Padding="20" Spacing="10">
  <Entry Placeholder="Username"
         VerticalOptions="Start"
         HorizontalOptions="FillAndExpand" />

  <Entry Placeholder="Password"
         VerticalOptions="Start"
         HorizontalOptions="FillAndExpand" />

  <Button Text="Submit" VerticalOptions="EndAndExpand" />
</StackLayout>