How to center a ImageView vertically in its parent?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

How to center a ImageView vertically in its parent?

Postby kknight » Sat Jan 23, 2010 5:47 am

My layout design is a TextView with long text and a ImageView to the right of the TextView. I want to center the ImageView in its parent vertically, so I used android:layout_centerVertical="true", but it turned out that the ImageView was aligned to the bottom of its parent. If I don't use layout_centerVertical property, the ImageView will be aligned to the top of its parent. How can I solve this problem? Thanks.

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4.  
  5.  
  6. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  7.  
  8.    android:layout_width="fill_parent" android:layout_height="wrap_content"
  9.  
  10.    android:background="#000000">
  11.  
  12.     <ImageView android:id="@+id/t3" android:layout_width="wrap_content"
  13.  
  14.        android:layout_height="wrap_content" android:src="@drawable/arrow_right"
  15.  
  16.        android:layout_alignParentRight="true" android:background="#ff0000"
  17.  
  18.        android:layout_centerVertical="true" android:scaleType="centerInside" />
  19.  
  20.     <TextView android:layout_width="fill_parent"
  21.  
  22.        android:layout_height="wrap_content"
  23.  
  24.        android:text="This a very very very very very very very very very very very very very very very very long sentence"
  25.  
  26.        android:textColor="#231ACC" android:layout_toLeftOf="@id/t3" />
  27.  
  28. </RelativeLayout>
  29.  
  30.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
kknight
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Jan 23, 2010 5:40 am

Top

Postby snowwire » Tue Feb 02, 2010 9:10 am

Hi kknight

First off, get rid of the RelativeLayout as it has a bug when you vertically center items on Android 1.5 phones. Use a LinearLayout instead with the orientation set to horizontal. For the vertical centering of the image, this takes some playing around. Here are two things you can try.

1. Set the image layout_height to fill_parent and also set it's android:gravity property.

or

2. Set the LinearLayout android:gravity property.

Hope this helps
snowwire
Freshman
Freshman
 
Posts: 6
Joined: Tue Feb 02, 2010 7:32 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 5 guests