【问题标题】:Xamarin grid size problems when used with resized ImageButton (with AspectFit)与调整大小的 ImageButton(使用 AspectFit)一起使用时的 Xamarin 网格大小问题
【发布时间】:2019-08-20 18:10:00
【问题描述】:

我想要的是两个并排的图像按钮,它们与屏幕底部的屏幕宽度相匹配。无论屏幕大小,我都希望纵​​横比相同。

I want it to look something like this

我尝试使用 Grid 来实现这一点。我有 2 个具有相同源图像的 ImageButton 元素。问题是当这些图像被调整为网格行的高度时,它们不会包裹新的图像高度,当它们被调整大小时,它们不会填满整个屏幕宽度。在这两种情况下,网格似乎都采用原始图像的高度。

Screen 1440 x 2960 in preview, when image is scaled down, grid height is bigger than it should be.

Screen 1800 x 2560 in preview, when image is supposed to be scaled up, but it seems that grid is clamping it to the original image height.

XAML:

<StackLayout 
Spacing="0">

<StackLayout
    VerticalOptions="Start">
</StackLayout>

<StackLayout
    VerticalOptions="CenterAndExpand">
</StackLayout>

<StackLayout
    Orientation="Horizontal"
    BackgroundColor="Bisque"
    HorizontalOptions="FillAndExpand"
    VerticalOptions="FillAndExpand">

    <Grid x:Name="grid"
        HorizontalOptions="FillAndExpand"
        VerticalOptions="FillAndExpand"
        ColumnSpacing="0"
        RowSpacing="0">

        <ImageButton
           x:Name="imageButton"                         
           Source="key1.png"
            Grid.Row="0"
            Grid.Column="0"/>

        <ImageButton                        
           Source="key1.png"
           Grid.Row="0"
           Grid.Column="1" />
    </Grid>

</StackLayout>

我找到了缩小图像大小时的解决方法:

imageButton.SizeChanged += (o, e) => 
        {
            grid.RowDefinitions.Clear();
            grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(imageButton.Height, GridUnitType.Absolute) });
        };

但这不是我想要的解决方案(特别是因为它在这两种情况下都不起作用)。我错过了什么?这应该很简单。这可能不是网格问题而是其他问题?我怎样才能做到这一点?

【问题讨论】:

    标签: xaml xamarin grid scale imagebutton


    【解决方案1】:

    无论屏幕大小,我都希望纵​​横比相同。

    尝试将RowDefinitionColumnDefinition 分配给Grid。并将VerticalOptions="EndAndExpand" 设置为 Grid 以使 imageButton 固定在屏幕底部。:

    <StackLayout 
            Spacing="0">
    
                    <StackLayout BackgroundColor="Yellow"
                         VerticalOptions="Start">
                    </StackLayout>
    
                    <StackLayout BackgroundColor="Green"
                            VerticalOptions="CenterAndExpand">
                    </StackLayout>
    
    
    
        <Grid x:Name="grid"
                            HorizontalOptions="FillAndExpand"
                            VerticalOptions="EndAndExpand"
                            ColumnSpacing="0"
                            RowSpacing="0">
    
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
    
            <ImageButton
                            x:Name="imageButton"                         
                            Source="Images.png"
                            Grid.Row="0"
                            Grid.Column="0"
                            Aspect="AspectFill"/>
    
            <ImageButton                        
                            Source="Images.png"
                            Grid.Row="0"
                            Grid.Column="1" 
                            Aspect="AspectFill"/>
        </Grid>
    </StackLayout>
    

    【讨论】:

    • 我需要我的图像来保持纵横比。如果我将 imageButton Aspect 设置为 AspectFill,它不会保持纵横比,它会拉伸并裁剪图像。我确实设置了ColumnDefinition Width="50*"RowDefinition Height="auto",但它并没有像我想象的那样工作。
    • 如果你想保持图像的纵横比,你应该使用AspectFit而不是AspectFillAspectFit :缩放图像以适合视图。有些部分可能是空的(字母装箱)。和AspectFill:缩放图像以填充视图。一些部分可能会被剪裁以填充视图。
    • 我确实使用了AspectFit,然后我遇到了上述问题,其中网格高度采用原始图像高度而不是调整大小。
    • 如果您使用 AspectFit,我认为网格不会采用调整后的高度,图像将被缩放而不是网格。
    • 还有其他方法可以实现吗?
    猜你喜欢
    • 1970-01-01
    • 2012-12-22
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多