2

I use android_weight to layout my widgets:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_weight="4">

This is the full XML layout: https://pastebin.com/7FqMuTdM

This shows properly in android studio designer. However, when shown in the Xamarin app, all the layout_weight attributes seem to be ignored.

What am I doing wrong? Is Xamarin able to recognize layout_weight?

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
janavarro
  • 869
  • 5
  • 24
  • 2
    This has nothing to do with Xamarin. Your layout is in a ScrollView, so layout_weight might not work as you think it does. I would also recommend converting all your messy and nested layout into a ConstraintLayout for much better performance than using layout_weights and spending a lot of layout and measure passes on calculating stuff. – Cheesebaron Aug 07 '19 at 06:54
  • Thanks for the suggestion of using constraint layout, once I learn how it works I will use it. You were right, it was because of the scrollview, I found this solution thanks to your suggestion: https://stackoverflow.com/questions/10312272/layout-weights-do-not-work-inside-a-scrollview – janavarro Aug 07 '19 at 07:11

1 Answers1

1

I am not very familiar with Xamarin. However, your weight setting has a possible error while you are setting up the width and height of the layout. If the parent layout is having a vertical orientation, you should not specify the height of the child layouts when you are specifying their heights by the weight attribute. Hence, if the parent layout orientation is vertical, the child layout you are referring to should look like the following.

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:gravity="center"
    android:layout_weight="4">

Note that, I have set the android:layout_height to 0dp so that it takes the width specified by the weight.

On the other hand, if the parent layout orientation is horizontal, you should not specify the width of the child layout and hence set the width of the layout to 0dp like the following.

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_weight="4">

Hope that helps!

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • 1
    I forgot you had to do that, but this wasn't the problem, I think weight overwrites the width and height attribute, now it is working thanks to Cheesebaron's comment. – janavarro Aug 07 '19 at 07:16