Center text in a list box vertically

I am trying to make a listview with text and image for each row. The image should be on the right, and the text should contain the rest of the line. The text should be centered vertically. I can do all this except the central one. This is what I had after many unsuccessful 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 text is always shown at the top of the line. I have already seen some reports that this may be due to the way the xml inflates when it is executed manually. But I do not. I leave it in the frame.

+6
source share
3 answers

I am using RelativeLayout which contains TextView and ImageView .

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

+9
source

add this to textview

 android:gravity="center_vertical" 

or

 android:gravity="center" 
+2
source

RelativeLayout user instead of LinearLayout Set fill_parent for TextView and wrap_content for ImageView.

+1
source

Source: https://habr.com/ru/post/909004/


All Articles