我需要像砖石风格(https://halcyon-theme.tumblr.com/)一样在内容页面上列出公告,但我无法成功。我的问题是,当我使用 Parent Stacklayout 的垂直对齐选项设置为 FillAndExpand 时,Scrollview 没有覆盖整个页面。但是如果我将 Scrollview 和 Parent StackLayout 设置为静态高度值,没关系,但由于每个项目的高度会改变,我不能像你猜的那样使用它。
<StackLayout x:Name="stckParent" Orientation="Vertical" VerticalOptions="FillAndExpand"> <Grid VerticalOptions="FillAndExpand" RowSpacing="0" ColumnSpacing="0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <StackLayout x:Name="stckLeft" Grid.Column="0" Margin="0,0,2,0" Orientation="Vertical" VerticalOptions="FillAndExpand"> </StackLayout> <StackLayout x:Name="stckRight" Grid.Column="1" Margin="2,0,0,0" Orientation="Vertical" VerticalOptions="FillAndExpand"> </StackLayout> </Grid> </StackLayout> </ScrollView>
public MainPage()
{
InitializeComponent();
var list = new ObservableCollection<Announcement>
{
new Announcement
{
ID = 1,
Title = "Yedek parça siparişlerinizde %22' ye varan indirim!",
ImagePath = "https://image5.sahibinden.com/photos/08/54/18/x5_719085418j7p.jpg",
IsActive = true,
CreateDate = DateTime.Now,
},
new Announcement
{
ID = 2,
Title = "Fren Balataları Hangi Sıklıkla Değiştirilmelidir?",
ImagePath = "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTJzQfduSrJ3Nh7SHzGSGrCmTnWses4AcfuRSnU7hX4y9XN4TSU&usqp=CAU",
IsActive = true,
CreateDate = DateTime.Now,
}
};
DeviceHelper helper = DependencyService.Get<IDeviceHelper>().GetDevice();
double height = 0;
for (int m = 0; m < 6; m++)
{
for (int i = 0; i < list.Count; i++)
{
Frame frame = new Frame
{
Padding = new Thickness(0, 0, 0, 0)
};
// Stack
StackLayout stack = new StackLayout
{
Margin = new Thickness(10),
};
// Image
Image image = new Image
{
Source = list[i].ImagePath,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand
};
// Title
Label title = new Label
{
Text = list[i].Title,
Margin = new Thickness(0, 6, 0, 0),
FontSize = 13
};
// Date
Label date = new Label
{
Text = list[i].CreateDate.ToString().Substring(0, 10), // temporary workaround
Margin = new Thickness(0, 6, 0, 0),
TextColor = Color.Gray,
FontSize = 11
};
stack.Children.Add(image);
stack.Children.Add(title);
stack.Children.Add(date);
frame.Content = stack;
if (i % 2 == 0)
{
stckLeft.Children.Add(frame);
}
else
{
stckRight.Children.Add(frame);
SizeRequest columnSizeRequest = frame.Measure(600, 800);
height += columnSizeRequest.Request.Height * 6;
}
}
}
stckLeft.HeightRequest = height;
stckRight.HeightRequest = height;
scrList.HeightRequest = helper.ScreenHeight - 100;
}
为什么 Scrollview 不能填满整个页面?能够做砖石风格页面的最佳实践是什么?如何获得刚刚绑定的帧大小?{
SizeRequest columnSizeRequest = frame.Measure(300, 400); // 结果总是41,没有意义..
height += columnSizeRequest.Request.Height * 6;
}
GitHub链接; https://github.com/Erdogan34/Test-Masonry
================= 更新 =================
弹性布局;如何将绿色框设置为顶部?
<FlexLayout Direction="Row" Wrap="Wrap" AlignContent="Start"
JustifyContent="Start" AlignItems="Start">
<Frame BackgroundColor="Red" Margin="4,4">
<StackLayout WidthRequest="140">
<Label Margin="0,10" Text="Deneme"/>
<Label Margin="0,10" Text="Deneme"/>
<Label Margin="0,10" Text="Deneme"/>
</StackLayout>
</Frame>
<Frame BackgroundColor="Blue" Margin="4,4">
<StackLayout WidthRequest="140">
<Label Margin="0,10" Text="Deneme"/>
<Label Margin="0,10" Text="Deneme"/>
<Label Margin="0,10" Text="Deneme"/>
<Label Margin="0,10" Text="Deneme"/>
<Label Margin="0,10" Text="Deneme"/>
</StackLayout>
</Frame>
<Frame BackgroundColor="Green" Margin="4,4" FlexLayout.Grow="1">
<StackLayout WidthRequest="140">
<Label Margin="0,10" Text="Deneme"/>
<Label Margin="0,10" Text="Deneme"/>
<Label Margin="0,10" Text="Deneme"/>
<Label Margin="0,10" Text="Deneme"/>
<Label Margin="0,10" Text="Deneme"/>
<Label Margin="0,10" Text="Deneme"/>
<Label Margin="0,10" Text="Deneme"/>
</StackLayout>
</Frame>
<Frame BackgroundColor="Yellow" Margin="4,4" FlexLayout.Grow="1">
<StackLayout WidthRequest="140">
<Label Margin="0,10" Text="Deneme"/>
<Label Margin="0,10" Text="Deneme"/>
<Label Margin="0,10" Text="Deneme"/>
</StackLayout>
</Frame>
</FlexLayout>