There is no layout_gravity attribute in RelativeLayout class and RelativeLayout.LayoutParams inner class. It only has android:gravity attribute. But still I am able to add layout_gravity attribute to RelativeLayout in xml file and make the layout change according to its parent. Look at the following code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="false"
android:background="@color/colorPrimaryDark"
android:text="First Child"
android:textAppearance="?android:textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/textView"
android:layout_toRightOf="@+id/textView"
android:background="@color/colorPrimaryDark"
android:text="Second child"
android:textAppearance="?android:textAppearanceLarge"/>
When I run this code, the RelativeLayout positions itself in the centre of the screen. I thought it will throw an error since RelativeLayout does not have layout_gravity attribute. So how is it possible to use layout_gravity in RelativeLayout?
Here's the link for RelativeLayout documentation https://developer.android.com/reference/android/widget/RelativeLayout.html. I searched for layout_gravity in it. But there's no layout_gravity in RelativeLayout.