简体   繁体   English

Xamarin.Forms ListView 中的项目之间的填充

[英]Padding between items in Xamarin.Forms ListView

I have defined the following Cell type in a Xamarin.Forms project:我在 Xamarin.Forms 项目中定义了以下 Cell 类型:

    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;
    }

This is called within a ListView, like so:这在 ListView 中调用,如下所示:

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

        Content = listView
     }

When this renders on the screen each item is really squashed up against its neighbours.当这在屏幕上呈现时,每个项目实际上都被它的邻居压扁了。 I tried adding padding to the StackLayout in the MyCell class, but doing so results in text going off the screen.我尝试向 MyCell 类中的 StackLayout 添加填充,但这样做会导致文本离开屏幕。 I'd like to have a gap between each item.我想在每个项目之间留一个间隙。

I'm think it may be worth converting the view to use Xaml to make this more clear, so if its easier to achieve as Xaml I'd accept that as an answer!我认为将视图转换为使用 Xaml 以使其更加清晰可能是值得的,所以如果它更容易实现为 Xaml,我会接受它作为答案!

Set the HasUnevenRows property to true on the ListView.在 ListView 上将 HasUnevenRows 属性设置为 true。 That way you have more control over the cell size.这样您就可以更好地控制单元格大小。

Also, leave RowHeight to its default value (-1).此外,将 RowHeight 保留为其默认值 (-1)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM