2016-09-15 8 views
0

私は3つのラベルを持っています - 私は2番目のラベルを中央に置き、1番目と3番目のラベルに同じサイズの他のスペースを塗りたいと思います。Xamarin.Formsはアンドロイドのレイアウトを中心にしていません

代わりに、私はここに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 }; 
関連する問題