Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center a ImageView vertically in its parent?

Tags:

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.

<?xml version="1.0" encoding="utf-8"?>  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent" android:layout_height="wrap_content"     android:background="#000000">     <ImageView android:id="@+id/t3" android:layout_width="wrap_content"         android:layout_height="wrap_content" android:src="@drawable/arrow_right"         android:layout_alignParentRight="true" android:background="#ff0000"         android:layout_centerVertical="true" android:scaleType="centerInside" />     <TextView android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="This a very very very very very very very very very very very very very very very very long sentence"         android:textColor="#231ACC" android:layout_toLeftOf="@id/t3" /> </RelativeLayout> 
like image 927
user256239 Avatar asked Jan 23 '10 02:01

user256239


People also ask

How do you center a vertical view?

The simple and quick answer is to add android:gravity="center_vertical" to the parent(containing) view.

How to set ImageView to center in android xml?

Try layout_gravity and gravity attributes in the LinearLayout to make all the childs to be centered by default. This above is a splash screen activity. So there are two childs - ImageView and TextView. Both are center aligned.


1 Answers

Try android:layout_gravity="center_vertical"

like image 109
Seshu Vinay Avatar answered Sep 28 '22 20:09

Seshu Vinay