2015-08-25 70 views
5

如何在Xamarin Forms水平StackLayout中均勻分配4個控件。我在iPhone 4S上看起來不錯,但在iPhone 6或iPad上很糟糕。水平堆棧佈局是ListView中的列表項。我已經嘗試了一個相對佈局和1行網格。但是我看起來最好(在4S上)。謝謝。如何均勻分佈Xamarin中的4個控件水平StackLayout

按順序,我有一個按鈕,標籤,標籤和一個按鈕。

 stackLayout = new StackLayout { 
      Orientation = StackOrientation.Horizontal, 
      HorizontalOptions = LayoutOptions.Center 
     }; 
     deleteButton.Image = "minus.png"; 
     deleteButton.HorizontalOptions = LayoutOptions.Start; 

     //label text width may vary from 4 to 16 characters 
     displayLabel.SetBinding(Label.TextProperty, "Text"); 
     displayLabel.HorizontalOptions = LayoutOptions.Center; 
     displayLabel.WidthRequest = 125; 

     //label text is always 8 characters 
     displayLabel2.SetBinding(Label.TextProperty, "Text2"); 
     displayLabel2.HorizontalOptions = LayoutOptions.Center; 

     button2.Image = "plus.png"; 
     button2.HorizontalOptions = LayoutOptions.EndAndExpand; 

     stackLayout.Children.Add(deleteButton); 
     stackLayout.Children.Add(displayLabel); 
     stackLayout.Children.Add(displayLabel2); 
     stackLayout.Children.Add(button2); 
+0

只是一個問題:這是否絕對需要一個StackLayout?如果沒有,那麼你可以考慮一個可以很容易地爲你處理這個問題的Grid。 – Demitrian

+0

我嘗試了一行4列的網格,但由於某種原因,4項沒有正確對齊。 –

回答

13

使用StackLayout:

using Xamarin.Forms; 

var label1 = new Label() { 
    Text = "label1", 
    HorizontalOptions = LayoutOptions.FillAndExpand, //OR CenterAndExpand 
}; 

var label2 = new Label() { 
    Text = "label2", 
    HorizontalOptions = LayoutOptions.FillAndExpand, //OR CenterAndExpand 
}; 

var label3 = new Label() { 
    Text = "label3", 
    HorizontalOptions = LayoutOptions.FillAndExpand, //OR CenterAndExpand 
}; 

var label4 = new Label() { 
    Text = "label4", 
    HorizontalOptions = LayoutOptions.FillAndExpand, //OR CenterAndExpand 
}; 

var stackLayout = new StackLayout 
{ 
    Orientation = StackOrientation.Horizontal, 
    HorizontalOptions = LayoutOptions.FillAndExpand, 
    Children = { 
     label1, 
     label2, 
     label3, 
     label4, 
    } 
}; 

RootPage.Children.Add(new ContentPage { 
    Padding = 10, 
    Content = stackLayout 
}); 

使用網格:

using Xamarin.Forms; 

    var label1 = new Label() { 
    Text = "label1", 
}; 

var label2 = new Label() { 
    Text = "label2", 
}; 

var label3 = new Label() { 
    Text = "label3", 
}; 

var label4 = new Label() { 
    Text = "label4", 
}; 

var gridLayout = new Grid 
{ 
    HorizontalOptions = LayoutOptions.FillAndExpand, 
    ColumnDefinitions = new ColumnDefinitionCollection() { 
     new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) }, 
     new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) }, 
     new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) }, 
     new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) }, 
    }, 
    RowDefinitions = new RowDefinitionCollection() { 
     new RowDefinition() { Height = GridLength.Auto } 
    } 
}; 

gridLayout.Children.Add(label1, 0, 0); 
gridLayout.Children.Add(label2, 1, 0); 
gridLayout.Children.Add(label3, 2, 0); 
gridLayout.Children.Add(label4, 3, 0); 

RootPage.Children.Add(new ContentPage { 
    Padding = 10, 
    Content = gridLayout 
}); 
+0

謝謝丹尼爾。 FillAndExpand是使用堆棧佈局的關鍵。我不得不給我的第二個項目的寬度請求(因爲它的寬度變化),以保持所有四個列在行與行之間對齊。我會很快嘗試你的CenterAndExpand版本和網格實現。 –

+0

沒問題。你可以在這裏閱讀更多關於LayoutOptions:http://stackoverflow.com/questions/25338533/what-is-the-difference-between-xamarin-forms-layoutoptions-especially-fill-and –

+0

快速跟進問題。第一項是動態的,因爲它僅在有時顯示有效的第一項(label2)時才顯示爲固定在行的最左端,我如何向左側添加一些填充或空格label2的? –