Xamarin.Forms - 将元素放置在另一个元素之上。

5
我在我的Xamarin.Forms项目中有一个页面,我需要它看起来像这样:

enter image description here

我可以定位红色和蓝色的框,没有问题。但我无法想出如何创建绿色的框。
我使用的XAML如下:
<StackLayout HorizontalOptions="FillAndExpand">
    <StackLayout VerticalOptions="FillAndExpand" BackgroundColor="Red">   
         //I'm thinking the green box should be positioned absolutely in here or something?         
    </StackLayout>
    <StackLayout VerticalOptions="End" BackgroundColor="Blue">
        <Grid VerticalOptions="EndAndExpand">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Button Grid.Column="0" Text="Button" HorizontalOptions="FillAndExpand" BorderRadius="0" VerticalOptions="EndAndExpand" HeightRequest="50" TextColor="White" BackgroundColor="#85C034"></Button>
        </Grid>
    </StackLayout>
</StackLayout>
1个回答

5
Grid 是实现此行为的最佳布局控件。您可以使用 * 将一行扩展到填充整个表格,将文本标签嵌套在第一行的内容视图中,然后将第二行设置为所需高度。

例如:

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:Grids"
    x:Class="Grids.GridsPage">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="60"/>
        </Grid.RowDefinitions>
        <ContentView 
            BackgroundColor="Red" Grid.Row="0">
            <Label TextColor="Black"
                Text="Text"
                Margin="0,0,0,10"
                BackgroundColor="#00FF02"
                VerticalOptions="End"
                HorizontalOptions="Center"/>
        </ContentView>
        <ContentView BackgroundColor="Blue" Grid.Row="1"/>
    </Grid>
</ContentPage>

enter image description here


2
好的,就快完成了。但是,我还需要一些东西来覆盖红色背景。比如一个图片或BoxView之类的东西,在其上面应该能看到绿色文本框。奇怪的是,当我将像<BoxView BackgroundColor="Purple" VerticalOptions="StartAndExpand"></ BoxView>这样的东西放在红色ContentView中时,它不会显示出来。 - Fayze

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接