在Xamarin.Forms中布局背景图像

时间:2017-05-07 18:30:28

标签: android xaml xamarin xamarin.forms

我需要具有固定高度(例如200)的带有背景图像的块,其应该AspectFill此块和StackLayout在此块底部具有未知高度。

我尝试使用RelativeLayout并将ImageStackLayout放入其中。图像放置得很完美,但我不知道如何在底部放置StackLayout

此布局包含两个Labels,因此我无法将其HeightConstraitYConstrait硬编码为常量,因为文本在不同平台和屏幕尺寸上可能具有不同的高度(或者,这可能是错的?)

我该怎么做?

enter image description here

1 个答案:

答案 0 :(得分:3)

替代方案我习惯了相对布局方法:

这基本上使用彼此重叠并填充可用空间的图像和堆栈布局。然后内部堆栈布局能够从末端扩展。如果要创建“最大”数量以向上扩展(例如,图像的最大50%),可以将外部布局更改为“layoutBounds 1,1,1,.5”

以下代码使用背景颜色,因此您可以在复制时轻松查看。 有很多修改选项,例如使用背景图像和stacklayout以外的项目,例如frame。

<AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
    <Image BackgroundColor="Red" AbsoluteLayout.LayoutBounds="1,1,1,1" AbsoluteLayout.LayoutFlags="All" Aspect="AspectFill"></Image>
    <StackLayout AbsoluteLayout.LayoutBounds="1,1,1,1" AbsoluteLayout.LayoutFlags="All" BackgroundColor="Transparent">
        <StackLayout BackgroundColor="Blue" HorizontalOptions="FillAndExpand" VerticalOptions="EndAndExpand" Orientation="Vertical">
            <Label Text="Label 1"></Label>
            <Label Text="Label 2"></Label>
        </StackLayout>
    </StackLayout>
</AbsoluteLayout>