填充

2017-02-10 57 views
1

我已經在Xamarin.Forms項目定義的下列細胞類型:填充

public MyCell() // constructor 
    { 
     var messageLabel = new Label 
     { 
      FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)), 
      FontAttributes = FontAttributes.Bold, 
     }; 
     messageLabel.SetBinding(Label.TextProperty, new Binding("Message")); 

     var dateLabel = new Label 
     { 
      FontSize = Device.GetNamedSize(NamedSize.Micro, typeof(Label)) 
     }; 
     dateLabel.SetBinding(Label.TextProperty, new Binding("Date")); 

     var view = new StackLayout 
     { 
      Children = { messageLabel, dateLabel }, 
      Orientation = StackOrientation.Vertical, 
     }; 

     View = view; 
    } 

這被稱爲一個ListView內,像這樣:

public MyPage() 
    { 
     var listView = new ListView() 
     { 
      ItemsSource = GetAllitems(), 
      ItemTemplate = new DataTemplate(typeof(MyCell)), 
     }; 

     Content = listView 
    } 

當這在屏幕上呈現出每個項目實際上已經與鄰居壓制在一起。我試圖在MyCell類的StackLayout中添加填充,但這樣做會導致文本離開屏幕。我想每個項目之間有差距。

我認爲這可能是值得轉換視圖使用Xaml使這更清晰,所以如果它更容易實現Xaml我會接受這個答案!

+0

你有沒有試過設置'ListView'的RowHeight? –

+0

這會修復所有人的身高嗎?所以如果一個物品包裝了一行文字,物品不會增加? – MartinM

+0

try ti將以下內容添加到您的ListView中Horizo​​ntalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand,然後添加一個填充 –

回答

1

在ListView上將HasUnevenRows屬性設置爲true。這樣你可以更好地控制單元大小。

另外,將RowHeight保留爲默認值(-1)。

+0

爲清晰起見編輯答案,因爲這對StackPanel中的Padding = new Thickness(15) – MartinM