首页 > 解决方案 > Xamarin 使用相对布局和背景图像形成页面布局

问题描述

我是 Xamarin 表单和 Xaml 的新手,尝试设计带有背景图像的页面布局,我发现使用相对布局,我可以使用 aspect 属性将图像扩展到整个屏幕。但是,当我在 RelativeLayout 中的图像之后放置网格或布局时,它不会扩展到整个区域,它只会覆盖给定的内容。

我试图使用相对布局来实现这一点。我正在使用带有 aspect 属性的 Image 标签,然后在图像顶部使用我的 stacklayout。

 <RelativeLayout>
                <Image Aspect="AspectFill" Source="c1.jpg" />
                <StackLayout>
                    <Button Text="Meha" TextColor="White"/>
                </StackLayout>
 </RelativeLayout>

我希望按钮覆盖整个宽度并将其垂直居中对齐。

标签: xamlxamarin

解决方案


这就是我通常解决这种情况的方法:

        <Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="White">
            <Image  Source="c1.jpg" Aspect="AspectFill" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
            <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="Center">
                <Button Text="Meha" TextColor="Black" HorizontalOptions="FillAndExpand"/>
            </StackLayout>
        </Grid>

推荐阅读