Keep the RelativeLayout as small as possible, but increase the growth of children to fill all the free space

My RelativeLayout contains several ImageView / TextView controls of varying heights, for example. 20, 70, 35. Controls enclosed in a row. I want them to increase their height to the same maximum control height, for example. 70. The problem is that with my layout, the RelativeLayout container and all controls expand to the height of the screen.

<RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <ImageView android:src="@drawable/image1" android:background="#FFCCDDEE" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_centerVertical="true" android:layout_alignParentLeft="true" /> <ImageView android:src="@drawable/image2" android:background="#FFCCDDEE" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_centerVertical="true" android:layout_centerInParent="true" /> <ImageView android:src="@drawable/image3" android:background="#FFCCDDEE" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_centerVertical="true" android:layout_alignParentRight="true" /> </RelativeLayout> 

I can set the RelativeLayout height to 70, but I believe that I do not know how much the control size of the child is set in advance.

I can also achieve the desired effect using LinearLayout, but want to use RelativeLayout.

  <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <ImageView android:src="@drawable/image1" android:background="#FFCCDDEE" android:layout_width="wrap_content" android:layout_height="fill_parent" android:gravity="center_vertical" /> <ImageView android:src="@drawable/image2" android:layout_width="wrap_content" android:layout_height="fill_parent" android:gravity="center_vertical" /> <ImageView android:src="@drawable/image3" android:layout_width="wrap_content" android:layout_height="fill_parent" android:gravity="center_vertical" /> </LinearLayout> 

Any ideas?

+4
source share
1 answer

Have you ever thought about using padding / margins?

 <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <ImageView android:src="@drawable/image1" android:background="#FFCCDDEE" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:padding="70dip" /> .... 

This XML should create a 140 x 140 image (I think). It will not be dynamic, but XML is not capable of doing such dynamic things.

If you want to make something more dynamic, you can write code that would pull out all the sizes of your elements, find the largest one, and then set all your objects to that size.

This link may be useful for this.

Good luck

0
source

Source: https://habr.com/ru/post/1333336/


All Articles