Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between layout_gravity="center_vertical" and layout_centerVertical="true"

I have such layout

<?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:padding="@dimen/DP_3">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/image"
        android:layout_centerVertical="true"
        android:text="xxx"/>

    <TextView
        android:id="@+id/custom_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>

    <TextView
        android:id="@+id/text"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="yyy"/>

</RelativeLayout>

I want all children place vertically inside parent RelativeLayout, however, layout_centerVertical="true" did the trick, but layout_gravity="center_vertical" did nothing. I thought they got same meaning, but not really, did I miss understand sth?

like image 438
fifth Avatar asked Jul 31 '14 08:07

fifth


People also ask

What is the difference between attribute layout_gravity and gravity?

The difference between the gravity and layout_gravity XML attributes in Android is that the android:gravity attribute is used to arrange the position of the content inside a View (for example text inside a Button widget) while the android:layout_gravity is used to arrange the position of the entire View relative to ...

What is the difference between the two layout parameters layout_gravity and gravity?

The android:gravity attribute is used to arrange the position of the content inside a view (for example text inside a Button widget). The android:layout_gravity is used to arrange the position of the entire View relative to it's container.

What is the difference between gravity and layout gravity in android?

So in general android:layout_gravity attribute is used by child views to tell their parent how they want to be placed inside it, while android:gravity is used by the parent layout to tell the child views how they should be placed inside it.

What is android gravity?

android:gravity is an attribute that sets the gravity of the content of the view its used on. The android:gravity specifies how an object should position its content on both X and Y axis. The possible values of android:gravity are top, bottom, left, right, center, center_vertical, center_horizontal etc.


2 Answers

layout_gravity is the attribute for LinearLayout, which won't work in RelativeLayout.

If you change your RelativeLayout container to LinearLayout, the elements are positioned in center using android:layout_gravity="center_vertical"

Xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="xxx" 
        android:padding="5dip"/>

    <TextView
        android:id="@+id/custom_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="dsf" 
        android:padding="5dip"/>

    <TextView
        android:id="@+id/text"
        android:padding="5dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="yyy" />

</LinearLayout>

Output:

enter image description here

Reference Links:

android:layout_gravity

android:layout_centerVertical

like image 200
MysticMagicϡ Avatar answered Sep 28 '22 08:09

MysticMagicϡ


Well, it's easy:

layout_centerVertical="true"

did the trick... in a RelativeLayout

while

layout_gravity="center_vertical"

works in a LinearLayout.

like image 36
Phantômaxx Avatar answered Sep 28 '22 09:09

Phantômaxx