简体   繁体   中英

Xamarin Forms Relative Layout won't stack

With the following code:

<ScrollView Orientation="Vertical" Padding="0">
            <RelativeLayout BackgroundColor="Red" Padding="0">
                <BoxView Color="Blue" WidthRequest="100" HeightRequest="100" 
                RelativeLayout.XConstraint="{ConstraintExpression Type=Constant, Constant=0}" 
                RelativeLayout.YConstraint="{ConstraintExpression Type=Constant, Constant=0}" />
            </RelativeLayout>
            <RelativeLayout BackgroundColor="Green" Padding="0">
                <BoxView Color="Yellow" WidthRequest="100" HeightRequest="100" 
                RelativeLayout.XConstraint="{ConstraintExpression Type=Constant, Constant=0}" 
                RelativeLayout.YConstraint="{ConstraintExpression Type=Constant, Constant=0}" />
            </RelativeLayout>
        </ScrollView>

But for some reason, instead of stacking, each new relative layout takes up the whole screen like this:

在此处输入图片说明

Why won't they stack up vertically? Stack layouts would normally only take vertically or horizontally the combined height of their children, but this doesn't happen with relative layouts. What am I missing?

Try this layout. I added StackLayout in ScrollView and VerticalOptions="Start" for RelativeLayouts.

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="TestChat.ChatPage">
    <ContentPage.Content>
<ScrollView Orientation="Vertical" Padding="0">
    <StackLayout>
            <RelativeLayout BackgroundColor="Red" Padding="0" VerticalOptions="Start">
                <BoxView Color="Blue" WidthRequest="100" HeightRequest="100" 
                RelativeLayout.XConstraint="{ConstraintExpression Type=Constant, Constant=0}" 
                RelativeLayout.YConstraint="{ConstraintExpression Type=Constant, Constant=0}" />
            </RelativeLayout>
            <RelativeLayout BackgroundColor="Green" Padding="0" VerticalOptions="Start">
                <BoxView Color="Yellow" WidthRequest="100" HeightRequest="100" 
                RelativeLayout.XConstraint="{ConstraintExpression Type=Constant, Constant=0}" 
                RelativeLayout.YConstraint="{ConstraintExpression Type=Constant, Constant=0}" />
            </RelativeLayout>
            </StackLayout>
        </ScrollView>
    </ContentPage.Content>
</ContentPage>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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