2016-09-15 34 views
0

我有3个标签 - 我想居中第二个标签并填充第一个和第三个标签的大小相同的其他空间。Xamarin.Forms没有在android中集中布局

相反,我看到有使用LayoutOption.FillANdExpand

这里当字符串的代价是我的代码:

class MainPage : ContentPage 
{ 
    public MainPage() 
    { 
     StackLayout a = new StackLayout(); 
     a.Orientation = StackOrientation.Horizontal; 
     a.Spacing = 0; 
     a.Padding = new Thickness(0, 0, 0, 0); 
     a.HorizontalOptions = LayoutOptions.FillAndExpand; 

     Label l1 = new Label { Text = "1111", HorizontalOptions = LayoutOptions.FillAndExpand, HorizontalTextAlignment = TextAlignment.Center, BackgroundColor = Color.Red, }; 
     Label l2 = new Label { Text = "2222222222222", HorizontalOptions = LayoutOptions.Center, HorizontalTextAlignment = TextAlignment.Center, BackgroundColor = Color.Gray }; 
     Label l3 = new Label { Text = "333333333333", HorizontalOptions = LayoutOptions.FillAndExpand, HorizontalTextAlignment = TextAlignment.Center, BackgroundColor = Color.Red }; 

     a.Children.Add(l1); 
     a.Children.Add(l2); 
     a.Children.Add(l3); 
     Content = a; 
    } 
} 

的结果是这样的:enter image description here

我想111和33333 ...标签具有相同的尺寸并且2222标签位于我的页面中心...

回答

2

您可以使用Grid与3列:

<Grid> 
<Grid.ColumnDefinitions> 
    <ColumnDefinition/> 
    <ColumnDefinition Width="Auto"/> 
    <ColumnDefinition/> 
</Grid.ColumnDefinitions> 

<Label Text="1111" TextColor="White" BackgroundColor="Red" /> 
<Label Grid.Column="1" Text="2222222222222" TextColor="White" BackgroundColor="Gray" /> 
<Label Grid.Column="2" Text="33333" TextColor="White" BackgroundColor="Red" /> 
</Grid> 
0

虽然网格似乎工作正常,它有点开销。您可以尝试以下布局选项:

 Label l1 = new Label { Text = "1111", HorizontalOptions = LayoutOptions.StartAndExpand, HorizontalTextAlignment = TextAlignment.Center, BackgroundColor = Color.Red, }; 
     Label l2 = new Label { Text = "2222222222222", HorizontalOptions = LayoutOptions.Center, HorizontalTextAlignment = TextAlignment.Center, BackgroundColor = Color.Gray }; 
     Label l3 = new Label { Text = "333333333333", HorizontalOptions = LayoutOptions.EndAndExpand, HorizontalTextAlignment = TextAlignment.Center, BackgroundColor = Color.Red };