Every single ViewGroup
(e.g. LinearLayout
, RelativeLayout
, CoordinatorLayout
, etc.) needs to store information about its children’s properties. About the way its children are being laid out in the ViewGroup
. This information is stored in objects of a wrapper class ViewGroup.LayoutParams
.
To include parameters specific to a particular layout type, ViewGroups
use subclasses of ViewGroup.LayoutParams
class.
E.g. for
LinearLayout
it’s LinearLayout.LayoutParams
RelativeLayout
it’s RelativeLayout.LayoutParams
CoordinatorLayout
it’s CoordinatorLayout.LayoutParams
Most of ViewGroups
reutilize the ability to set margins
for their children, so they do not subclass ViewGroup.LayoutParams
directly, but they subclass ViewGroup.MarginLayoutParams
instead (which itself is a subclass of ViewGroup.LayoutParams
).
LayoutParams
in xml
LayoutParams
objects are created based on the inflated layout xml
file.
<LinearLayout xmlns:android="<http://schemas.android.com/apk/res/android>"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="right"
android:gravity="bottom"
android:text="Example text"
android:textColor="@android:color/holo_green_dark"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/holo_green_dark"
android:scaleType="centerInside"
android:src="@drawable/example"/>
</LinearLayout>
All parameters that begin with layout_
specify how the enclosing layout should work. When the layout is inflated, those parameters are wrapped in a proper LayoutParams
object, that later will be used by the Layout
to properly position a particular View
within the ViewGroup
. Other attributes of a View
are directly View
-related and are processed by the View
itself.
For TextView
:
layout_width
, layout_height
and layout_gravity
will be stored in a LinearLayout.LayoutParams
object and used by the LinearLayout
gravity
, text
and textColor
will be used by the TextView
itselfFor ImageView
:
layout_width
, layout_height
and layout_weight
will be stored in a LinearLayout.LayoutParams
object and used by the LinearLayout
background
, scaleType
and src
will be used by the ImageView
itself