I have a LinearLayout under a LinearLayout (master layout), both vertical orientation. I set the master weight sum to 1 and I add the child layout using code with weight of 0.8 and set the width to 0 and the height to MatchParent. But when I run the app it just doesnt show up, by debugging I could find that the width stays 0. Also, when the master layout orientation set to horizontal, it works fine. Why this happens?
This is the xml for the master layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1.0"
android:id="@+id/main_layout">
</LinearLayout>
The code:
In MainActivity.cs: (This ones just adds the Ore which is extending LinearLayout)
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
_activity = this;
CurrentOre = 0;
LinearLayout layout = FindViewById<LinearLayout>(Resource.Id.main_layout);
Ore current = OresHandler.GetAllOres()[CurrentOre];
layout.AddView(current);
}
The "child" layout: Ore class (extends LinearLayout):
LayoutParameters = new LinearLayout.LayoutParams(0, LayoutParams.MatchParent, 1F);
Orientation = Orientation.Vertical;
WeightSum = 1F;
SetHorizontalGravity(GravityFlags.Center);
// The name
TextView title = new TextView(Context);
title.Text = Name;
title.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, 0, 0.08F) ;
title.TextSize = 24;
title.TextAlignment = TextAlignment.Center;
title.SetBackgroundColor(Color.Transparent);
AddView(title);
// The image button
Button = new ImageButton(Context);
Button.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, 0,0.5F);
Button.SetScaleType(ScaleType.FitXy);
Button.SetBackgroundColor(Color.Transparent);
Button.SetImageResource(ImgId);
Button.Click += (sender, e) => Damage(1);
AddView(Button);
AddView(HealthHandler);
(I only placed relevant pieces of code) As you can see, the child layout (Ore) contains a TextView, ImageButton and another component which I created. But I do not think they matter at any way since the program works on horizontal.
Solved. LinearLayout only arranges the size depending on the orientation, so had to add a wrapping LinearLayout with horizontal orientation to change width size.