我必须定义以下内容页面:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="OrganizeMyLeague.Subscription"
x:DataType="Line"
Title="Subscription">
<VerticalStackLayout
Padding="5,0"
Spacing="5">
<Label Text="Account Information"
Style="{StaticResource Headline}"
TextColor="Blue"
HorizontalOptions="Center"
SemanticProperties.HeadingLevel="Level1"/>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="250" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" HorizontalTextAlignment="End" VerticalOptions="Center" Text="First Name" FontSize="16" TextColor="Black" HorizontalOptions="Start" />
<Entry Grid.Row="0" Grid.Column="1" x:Name="txtFirstname" Placeholder="Enter First Name" PlaceholderColor="#C7C7C7" WidthRequest="150" VerticalOptions="Center" FontSize="16" TextColor="Blue" HorizontalOptions="End" />
<Label Grid.Row="1" Grid.Column="0" HorizontalTextAlignment="End" VerticalOptions="Center" Text="Last Name" FontSize="16" TextColor="Black" HorizontalOptions="Start" />
<Entry Grid.Row="1" Grid.Column="1" x:Name="txtLasttname" Placeholder="Enter Last Name" PlaceholderColor="#C7C7C7" WidthRequest="150" VerticalOptions="Center" FontSize="16" TextColor="Blue" HorizontalOptions="End" />
<Label Grid.Row="2" Grid.Column="0" HorizontalTextAlignment="End" VerticalOptions="Center" Text="Address" FontSize="16" TextColor="Black" HorizontalOptions="Start" />
<Entry Grid.Row="2" Grid.Column="1" x:Name="txtAddress" Placeholder="Enter Address" PlaceholderColor="#C7C7C7" WidthRequest="150" VerticalOptions="Center" FontSize="16" TextColor="Blue" HorizontalOptions="End" />
<Label Grid.Row="3" Grid.Column="0" HorizontalTextAlignment="End" VerticalOptions="Center" Text="City" FontSize="16" TextColor="Black" HorizontalOptions="Start" />
<Entry Grid.Row="3" Grid.Column="1" x:Name="txtCity" Placeholder="Enter City" PlaceholderColor="#C7C7C7" WidthRequest="150" VerticalOptions="Center" FontSize="16" TextColor="Blue" HorizontalOptions="End" />
<Label Grid.Row="4" Grid.Column="0" HorizontalTextAlignment="End" VerticalOptions="Center" Text="Select State" FontSize="16" TextColor="Black" HorizontalOptions="Start" />
<Picker Grid.Row="4" Grid.Column="1" x:Name="txtState" WidthRequest="150" VerticalOptions="Center" FontSize="16" TextColor="Blue" HorizontalOptions="End" />
<Label Grid.Row="5" Grid.Column="0" HorizontalTextAlignment="End" VerticalOptions="Center" Text="Zip Code" FontSize="16" TextColor="Black" HorizontalOptions="Start" />
<Entry Grid.Row="5" Grid.Column="1" x:Name="txtZipCode" Placeholder="Enter Zip Code" PlaceholderColor="#C7C7C7" WidthRequest="150" VerticalOptions="Center" FontSize="16" TextColor="Blue" HorizontalOptions="End" />
</Grid>
</VerticalStackLayout>
</ContentPage>
显示方式如下:
我无法影响网格中的第二列,无论我做什么,我都无法让该列更靠近标签。我更希望输入控件精确地出现在标签的右侧,但显然没有办法做到这一点。我只能让第二列(第 1 列)更靠近标签文本。
要将元素彼此相邻放置,请使用
HorizontalStackLayout
单列网格虽然你确实
HorizontalOptions="Center"
为你的设置了Label
,但你并没有为你的设置,Grid
即你需要这是因为您的 ColumnDefinitions 100, 250 与默认的 HorizontalOptions 结合导致了您所描述的空间问题。