简体   繁体   English

如何使用 xamarin 在 StackLayout 中放置 5 列而不超出框架

[英]How to fit 5 columns in StackLayout without going out of the frame using xamarin

I want to put 5 columns together in StackLayout without going out of the frame.我想在 StackLayout 中将 5 列放在一起而不会超出框架。 In iPhone 12Pro Max everything is ok, but in Iphone 8 things don't look good.在 iPhone 12Pro Max 中一切正常,但在 iPhone 8 中看起来不太好。

iPhone8 模拟器

My Code:我的代码:

<Frame
                   BorderColor="White"
                   Margin="10,0,10,0"
                   CornerRadius="10"
                   HasShadow="True"
                   BackgroundColor="Transparent">
                 <RelativeLayout>
        <Image
            x:Name="ImageDescriptionForecast1"
            Aspect="AspectFill"
            RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,
                                                                   Property=Height,
                                                                   Factor=1}"
            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,
                                                                  Property=Width,
                                                                  Factor=1}"/>
        <StackLayout>
                    <Label x:Name="DateTimeForecast1"
                               Style="{StaticResource labelStyle}"
                               HorizontalTextAlignment="Center"
                               VerticalTextAlignment="Center"
                               VerticalOptions="Center"
                               FontSize="14"/>
                    <Label x:Name="DateTimeForecast2"
                               Style="{StaticResource labelStyle}"
                               HorizontalTextAlignment="Center"
                               FontSize="14"/>
                    <Grid BackgroundColor="Transparent" 
                      Padding="0,0,0,0"
                          RowSpacing="0"
                          ColumnSpacing="0">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="auto" />
                        </Grid.RowDefinitions>

                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        ........
                        ........
                        </Grid>
                <StackLayout>
            </RelativeLayout>
         </Frame>

I tried to put "auto" on ColumnDefinition Width = "auto" but this not work for me.我试图在 ColumnDefinition Width = "auto" 上加上“auto”,但这对我不起作用。 Is there a way to fit them on display?有没有办法把它们放在显示器上?

When we check document Xamarin.Forms Grid ,we will find that当我们检查文档Xamarin.Forms Grid时,我们会发现

在此处输入图像描述

So when the size of the item in the grid is large enough, it will take up a lot of space in the space even if we set value auto to ColumnDefinition .所以当网格中item的尺寸足够大的时候,即使我们将值auto设置为ColumnDefinition ,也会占用空间很大的空间。

If we want the items in the same row take up the same space, we usually set * for ColumnDefinition .如果我们希望同一行中的项目占用相同的空间,我们通常为ColumnDefinition设置* In this condition, if a row have many items and a single item take up a lot of space, all elements in a row will not be fully displayed on the screen.在这种情况下,如果一行中有很多项目,而单个项目占用大量空间,则一行中的所有元素都不会完全显示在屏幕上。

In this condition, we need to adjust the HeightRequest and WidthRequest of the item.在这种情况下,我们需要调整item的HeightRequestWidthRequest

For example:例如:

   <Frame Grid.Row="0"  
          HeightRequest="30" WidthRequest="30"
          BackgroundColor="Yellow"
         <Grid.Column="0">
            <Image Source="watermelon.png" HorizontalOptions="FillAndExpand" 
                   VerticalOptions="FillAndExpand" Aspect="AspectFill" />
   </Frame>

For some mobile desktop applications, if there are more columns, each item will be resized to a smaller size.对于一些移动桌面应用程序,如果有更多的列,每个项目将被调整为更小的尺寸。 Similarly, if there are fewer columns, then each item will be resized to a larger size.同样,如果列数较少,则每个项目将调整为更大的大小。

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

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