Search code examples
android-layoutxamarinxamarin.androidfluid-layoutstretch

Xamarin.Android layout to occupy all the available space?


Please help me achieve this layout, I need 3 ImageViews and a TextView in the same line with the last two aligned to the right side. I have tried put them into LinearLayout, GridLayout, TableLayout, RelativeLayout but for the love of god I can't figure this out!

Thank you!

enter image description here


Solution

  • LinearLayout( proportion):

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <ImageView
            android:layout_margin="5dp"
            android:src="@drawable/ic_launcher_background"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="50dp" />
        <TextView
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:text="TextView"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="50dp"
            android:background="@color/colorAccent"/>
        <View
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="50dp"/>
        <ImageView
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:src="@drawable/ic_launcher_background"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="50dp" />
        <ImageView
            android:layout_margin="5dp"
            android:src="@drawable/ic_launcher_background"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="50dp" />
    </LinearLayout>
    

    RelativeLayout( no proportion):

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <ImageView
            android:layout_margin="5dp"
            android:id="@+id/iv1"
            android:src="@drawable/ic_launcher_background"
            android:layout_width="50dp"
            android:layout_height="50dp" />
        <TextView
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_toRightOf="@id/iv1"
            android:id="@+id/tv"
            android:text="TextView"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:background="@color/colorAccent"/>
        <ImageView
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_toLeftOf="@id/iv3"
            android:id="@+id/iv2"
            android:src="@drawable/ic_launcher_background"
            android:layout_width="50dp"
            android:layout_height="50dp" />
        <ImageView
            android:layout_margin="5dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:id="@+id/iv3"
            android:src="@drawable/ic_launcher_background"
            android:layout_width="50dp"
            android:layout_height="50dp" />
    </RelativeLayout>
    

    Update:

            var relativeLayout = new Android.Widget.RelativeLayout(this)
            {
                LayoutParameters = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent),
                LayoutDirection = LayoutDirection.Ltr
            };
    
            var imageView1 = new ImageView(this);
            imageView1.SetImageResource(Resource.Drawable.icon);
            var layoutParams = new Android.Widget.RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
            layoutParams.SetMargins(5, 5, 5, 5);
            imageView1.Id = 1;
            imageView1.LayoutParameters = layoutParams;
            relativeLayout.AddView(imageView1);
    
    
            View childView1 = relativeLayout.GetChildAt(0);
            var textView = new TextView(this)
            {
                Text = "TextView"
            };
            layoutParams = new Android.Widget.RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
            layoutParams.AddRule(LayoutRules.RightOf, childView1.Id);
            layoutParams.SetMargins(0, 5, 5, 5);
            textView.Id = 2;
            textView.LayoutParameters = layoutParams;
            relativeLayout.AddView(textView);
    
            var imageView3 = new ImageView(this);
            imageView3.SetImageResource(Resource.Drawable.icon);
            layoutParams = new Android.Widget.RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
            layoutParams.AddRule(LayoutRules.AlignParentEnd);
            layoutParams.AddRule(LayoutRules.AlignParentRight);
            imageView3.Id = 3;
            imageView3.LayoutParameters = layoutParams;
            relativeLayout.AddView(imageView3);
    
            View childView2 = relativeLayout.GetChildAt(2);
            var imageView2 = new ImageView(this);
            imageView2.SetImageResource(Resource.Drawable.icon);
            layoutParams = new Android.Widget.RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
            layoutParams.AddRule(LayoutRules.LeftOf, childView2.Id);
            imageView2.Id = 4;
            imageView2.LayoutParameters = layoutParams;
            relativeLayout.AddView(imageView2);