RelativeLayout center vertical
Solution 1
I have edited your layout. Check this code now.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#33B5E5"
android:padding="5dp" >
<ImageView
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:background="@android:drawable/ic_lock_lock" />
<TextView
android:id="@+id/func_text"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_gravity="center_vertical"
android:layout_toRightOf="@+id/icon"
android:gravity="center"
android:padding="5dp"
android:text="This is my test string............"
android:textColor="#FFFFFF" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_gravity="center_vertical"
android:src="@android:drawable/ic_media_next" />
</RelativeLayout>
Solution 2
use
android:layout_centerVertical="true"
Solution 3
If View's height/width = wrap_content
use:
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
If View's height/width = match_parent
use:
android:gravity="center_vertical|center_horizontal"
Solution 4
For me, I had to remove
<item name="android:gravity">center_vertical</item>
from RelativeLayout, so children's configuration would work:
<item name="android:layout_centerVertical">true</item>
Solution 5
Try aligning top and bottom of text view to one of the icon, this will make text view sharing same height as them, then set gravity
to center_vertical
to make the text inside text view center vertically.
<TextView
android:id="@+id/func_text" android:layout_toRightOf="@id/icon"
android:layout_alignTop="@id/icon" android:layout_alignBottom="@id/icon"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:gravity="center_vertical" />
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on December 01, 2020Comments
-
Admin over 3 years
I want to make a list row layout. This layout has a imageview in the most left, a textview right next to the imageview, and a imageview in the most right. I want all of them are center vertical.
<RelativeLayout android:layout_width="fill_parent" android:layout_height="100dp" android:gravity="center_vertical" > <ImageView android:id="@+id/icon" android:layout_width="50dp" android:layout_height="50dp" android:layout_gravity="center_vertical" /> <TextView android:id="@+id/func_text" android:layout_toRightOf="@id/icon" android:layout_width="wrap_content" android:layout_height="100dp" android:layout_gravity="center_vertical" /> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentRight="true" android:layout_gravity="center_vertical" android:src="@drawable/arrow" /> </RelativeLayout>
I also tried to add
android:layout_centerVertical="true"
to the textview, but the result is the textview align bottom with the two imageview. I tried this in android 4.2 emulator. Anybody could help me about this? -
MightySeal almost 10 yearsThis should be the correct answer, cause RelativeLayout doesn't have layout_gravity attribute.
-
Acauã Pitta over 4 years"invalid param in a linear layout"
-
mufazmi over 3 yearsIf it's an answer, please right down the some description/information with your answer.