Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertically center a text on a listview row

I'm trying to make a listview with a text and an image for each row. The image should be on the right and the text should ocupy the rest of the row. The text should be verticaly centered. I can do all this except the centered part. This is what I have after many failed attempts:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  android:id="@+id/widget71"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:gravity="center_vertical">
<TextView
  android:id="@android:id/text1"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent"
  android:layout_marginTop="6px"
  android:textAppearance="?android:attr/textAppearanceLarge"
  android:text="@+id/text1"
  android:layout_weight="01"
  android:layout_gravity="center_vertical|fill_vertical|center_horizontal|center|clip_vertical"/>
<ImageView
  android:id="@android:id/icon"
  android:layout_width="30px"
  android:layout_height="40px"
  android:layout_marginTop="8px"
  android:layout_marginLeft="4px"
  android:layout_marginRight="8px"
  android:src="@drawable/delete"
  android:layout_gravity="right" />
</LinearLayout>

No matter what the text is allways shown on top of the row. I've already seen some reports that this may happen because of the way the xml is inflated when it's done manually. But I don't do that. I leave it to the framework.

like image 610
Nahoot Avatar asked Feb 21 '12 18:02

Nahoot


1 Answers

I use a RelativeLayout that contains a TextView and an ImageView.

For both I use the android:layout_centerVertical="true" attribute. Works fine for me.

like image 178
Squonk Avatar answered Sep 24 '22 02:09

Squonk