RelativeLayout does not stretch on Android

VladislavKasianenkoVladislavKasianenko UAMember ✭✭
edited June 2017 in Xamarin.Forms

Hello.
I have a problem with Android.
Here's what I have:

    <RelativeLayout BackgroundColor="Blue">
    <StackLayout Margin="2" BackgroundColor="{StaticResource Medium}"
    HeightRequest="{Binding Source={x:Reference LabelMessage}, Path=Height}"
    RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.75, Constant=0}"
    RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.25, Constant=0}">
    <Label x:Name="LabelMessage" Text="{Binding Message}" Margin="5" />
    </StackLayout>
    </RelativeLayout>

I had this problem on Windows Phone either, but after I added HeightRequest everything become fine. But Android.
Here's what I expect (and what is working on WP):

And here's the same code on Android:

The problem only in the text, it doesn't fit.

As for positioning (on WP it's right, on Android it's left - it's OK).

UPD
This my code under ListView > DataTemplate > ViewCell. The problem was in ViewCell. So set HasUnevenRows="True" on ListView. That helped me.

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Have you tried setting the VerticalOptions to any of the Expand variations, such as StartAndExpand, so it know that's its supposed to expand to fill available space?

  • VladislavKasianenkoVladislavKasianenko UAMember ✭✭

    Yes, I tried. It made no changes.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Hmmm... HeightRequest might be overriding it. Try the VerticalOptions without trying to set the Height in any way.
    Be sure to set all the parent controls all the way up - at least as a test.
    In other words, you have to set it on the Label or it won't know to expand.
    And on the StackLayout so it can expand to accommodate.
    And so on, so everything know to expand big enough.

    I also set hte background color to something lavish while testing. The StackLayout to lime and the RelativeLayout to fuschia etc, just so I can see where every control really exists and how big it really is.

  • VladislavKasianenkoVladislavKasianenko UAMember ✭✭

    Without HeightRequest it doesn't work at all.

    Also I tried to set up VerticalOptions="StartAndExpand" on all: Label, StackLayout and RelativeLayout. The same result. Android still the same. What interesting is that RelativeLayout is always the same, no matter it's long Lorem ipsum or just qwerty.

    It's the way I got HeightRequest for WP:

    @ClintStLaurent said:
    I also set the background color to something lavish while testing. The StackLayout to lime and the RelativeLayout to fuschia etc, just so I can see where every control really exists and how big it really is.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Well... I'm out of ideas. I don't do RelativeLayout ever so I'm not familiar with any specific issues that might be part of using it.

  • VladislavKasianenkoVladislavKasianenko UAMember ✭✭
    edited June 2017

    Either do I. :(

    @ClintStLaurent said:
    I'm out of ideas.

    Anyway, thank you.

  • AlenTomaAlenToma USMember ✭✭
    edited May 30

    You could Try this, it worked for me

      RelativeLayout.SetWidthConstraint(RightContent, Constraint.RelativeToParent((parent) =>
            {
             return Application.Current.MainPage.Width;
    
            }));
    
Sign In or Register to comment.