Vertical centering in a nested RelativeLayout inside a ListView

I tried to insert two RelativeLayouts as ListView strings and center the contents of the nested layout vertically. No matter what I tried, I could not get it to work:

screenshot:

This is my XML layout, I know this is a bit of a mess (also tried LinearLayouts without success):

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="fill_parent" android:descendantFocusability="blocksDescendants"> <CheckBox android:id="@+id/check" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_centerVertical="true" android:layout_marginTop="2dp" /> <TextView android:id="@+id/label" android:layout_toRightOf="@id/check" android:layout_toLeftOf="@+id/date_time_container" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_centerVertical="true" android:ellipsize="end" android:paddingLeft="6dp" android:paddingRight="6dp" android:singleLine="true" android:textAppearance="?android:attr/textAppearanceLarge" android:bufferType="spannable"> </TextView> <RelativeLayout android:id="@+id/date_time_container" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_centerVertical="true" android:background="@color/grey" > <TextView android:id="@+id/date_time" android:layout_toLeftOf="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:background="@color/green" android:layout_marginTop="2dp" android:bufferType="spannable" android:text=""> </TextView> <ImageView android:id="@+id/icon" android:layout_toRightOf="@id/date_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="4dp" android:layout_marginRight="0dp" android:layout_marginTop="2dp" android:layout_centerVertical="true" android:src="@drawable/urgent" android:contentDescription="@string/icon"/> </RelativeLayout> </RelativeLayout> 

I have already made sure that the strings in the adapter are correctly inflated, as was suggested in other threads:

 @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return mInflater.inflate(R.layout.task_row, parent, false); } 

Any ideas? I am sure that this should be something insignificant. Thanks!

Edit: In Eclipse, the layout is displayed correctly, it seems that nesting the layout inside the ListView row somehow messed up the layout: enter image description here

Edit 2: I thought I was using EditText somewhere. But if there is no way to make it look like a TextView, I will return to the drawing board :(

Edit 3: I finally solved this using LinearLayout and android: gravity = "center_vertical" in a TextView:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:descendantFocusability="blocksDescendants"> <CheckBox android:id="@+id/check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginTop="2dp" /> <TextView android:id="@+id/label" android:layout_toRightOf="@id/check" android:layout_toLeftOf="@+id/date_time_container" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:ellipsize="end" android:paddingLeft="6dp" android:paddingRight="6dp" android:singleLine="true" android:text="Some text" android:textAppearance="?android:attr/textAppearanceLarge" android:bufferType="spannable"> </TextView> <LinearLayout android:id="@+id/date_time_container" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@color/grey" > <TextView android:id="@+id/date_time" android:layout_width="wrap_content" android:layout_height="match_parent" android:bufferType="spannable" android:singleLine="true" android:gravity="center_vertical" android:background="@color/green" android:text="20:00" android:textAppearance="?android:attr/textAppearanceSmall" /> <ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/urgent" android:contentDescription="@string/icon"/> </LinearLayout> </RelativeLayout> 
+4
source share
1 answer

Try setting layout_height in the views inside the RelativeLayout root to wrap_content .

0
source

All Articles