Understand Android layout internals

satya - Wed Oct 17 2012 13:44:05 GMT-0400 (Eastern Daylight Time)

Here is the documentation page from Android

Here is the documentation page from Android

satya - Wed Oct 17 2012 13:48:03 GMT-0400 (Eastern Daylight Time)

A layout object is a view group

A layout object is a view group

satya - Wed Oct 17 2012 13:50:04 GMT-0400 (Eastern Daylight Time)

On layout parameters of a view

..attributes are considered "layout parameters," which are attributes that describe certain layout orientations of the View object, as defined by that object's parent ViewGroup object.

satya - Wed Oct 17 2012 13:55:21 GMT-0400 (Eastern Daylight Time)

If a layout has 5 children

A layout may obligate each view object (the 5) some characteristics that tells the parent layout the desired behavior for that view object.

So these parameters will apply irrespective of the type of the view object. So the contract of the layout is defined by the parent layout object and not individual member view.

This means even future views that are not yet built are expected to carry, albeit a hash table, of these layout parameters.

Not only that even an existing view may participate in multiple layouts. Depending on which layout it is participating the XML allowed for the layout parameters is controlled by the parent layout.

satya - Wed Oct 17 2012 14:09:18 GMT-0400 (Eastern Daylight Time)

Each layout is a viewgroup

Each layout is a viewgroup

satya - Wed Oct 17 2012 14:10:59 GMT-0400 (Eastern Daylight Time)

Each layout implements a subclass inheritng from ViewGroup.LayoutParams: example


class LinearLayout.LayoutParams 
    extends ViewGroup.LayoutParams
class RelativeLayout.LayoutParams
    extends ViewGroup.Layoutparams

....
etc.

satya - Wed Oct 17 2012 14:12:29 GMT-0400 (Eastern Daylight Time)

These come all the way from ViewGroup.LatyoutParams


layout_width
layout_height

satya - Wed Oct 17 2012 14:13:44 GMT-0400 (Eastern Daylight Time)

Some of the recognized values are


actual pixels
density independent pixels: dp
match_parent
wrap_content

satya - Wed Oct 17 2012 14:16:58 GMT-0400 (Eastern Daylight Time)

Figuring out view positions


getWidth
getHeight
getLeft (relative to parent x position)
getTop (relative to parent y position)
getRight
getBottom

satya - Wed Oct 17 2012 14:18:32 GMT-0400 (Eastern Daylight Time)

A view's wish: How large of a space do I want


getMeasuredWidth
getMeasuredHeight

satya - Wed Oct 17 2012 14:22:52 GMT-0400 (Eastern Daylight Time)

A view only has padding methods, but no margin


setPadding
getPadding

satya - Wed Oct 17 2012 14:23:08 GMT-0400 (Eastern Daylight Time)

Margin is outside of a view and padding in side of a view for html

Margin is outside of a view and padding in side of a view for html

satya - Wed Oct 17 2012 14:24:12 GMT-0400 (Eastern Daylight Time)

Margin can be controlled by viewgroup margin layout params

Margin can be controlled by viewgroup margin layout params

satya - Wed Oct 17 2012 14:28:55 GMT-0400 (Eastern Daylight Time)

Implemented by ViewGroup.MarginLayoutParams


layout_marginTop
layout_marginBottom
layout_marginLeft
layout_marginRight

This class is inherited from LayoutParams. All subsequent derivations like LinearLayoutParams are extending this one with margins. So most of the layouts get margins.