How to let ImageView and TextView be the same height

My layout has ImageView (iv) and TextView (tv). I want tv to be equal to IV. And the height of the TV is the same as that of IV. Gravity Television is set to CENTER_VERTICAL.

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true"/> <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/iv" android:text="TextView" android:gravity="center_vertical"/> </RelativeLayout> 

How to achieve the goal?

+6
source share
4 answers

Hi, try this .....

  <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:src="@drawable/ic_title_home_demo" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/iv" android:layout_alignTop="@+id/iv" android:layout_toRightOf="@+id/iv" android:gravity="center_vertical" android:text="TextView" /> </RelativeLayout> 
+20
source

You must set the textview property to android:layout_alignTop="@id/iv" and android:layout_alignBottom="@id/iv"

+4
source

To get the vertical center value, you need to remove the android:layout_alignParentTop="true" from your TextView tag.

However, it looks like you are trying to force the text to resize to fit the entire height of the ImageView. This, obviously, will not solve this, but I would not recommend doing this in the first place, as this could lead to some strange views.

+1
source

You can use different sizes using

Android: layout_width = "wrap_content"

and / or

Android: Height = "300dp"

(same for height). Also check the margins and gaskets.

If you need to be more dynamic, you can consider android: layout_weight ie so that the two buttons are of equal width and fill the horizontal space of the parent view that you set:

Android: layout_height = "wrap_content"

android_layout :. Weight = "0.5"

you can follow this link: How to make two different layouts of the same height?

+1
source

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


All Articles