brian
brian

Reputation: 6912

How to let the ImageView and the TextView the same height

My layout have ImageView(iv) and TextView(tv). I want to let the tv right of iv. And the tv's height is same as iv's. The tv Gravity set as 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 arrive the goal?

Upvotes: 8

Views: 10768

Answers (4)

loks
loks

Reputation: 490

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>

Upvotes: 25

Dharmendra
Dharmendra

Reputation: 33996

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

Upvotes: 4

Horrorgoogle
Horrorgoogle

Reputation: 7868

You can use distinct sizes by using

android:layout_width="wrap_content"

and /or

android:height="300dp"

(same for height). Also check margins and paddings.

If you need to be more dynamic, you might consider android:layout_weight i.e. for letting 2 buttons have equal width and fill the horizontal space of the parent view you would set:

android:layout_height="wrap_content"

android_layout:weight="0.5".

you can follow this link: How to make two different layout to have same height?

Upvotes: 1

Alex Paino
Alex Paino

Reputation: 250

Well to get it to center vertically correct, you will need to remove the android:layout_alignParentTop="true" line from your TextView tag.

However, it sounds like you might be trying to make the text change its size to fit the entire height of the ImageView. This obviously won't solve that, but I wouldn't recommend doing that in the first place as it could result in some strange looking views.

Upvotes: 1

Related Questions