Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advanced Explanation of Android Layout Properties?

I'm on a quest to learn how to layout components properly in Android. I'm a seasoned CSS/MXML developer and am having the hardest time getting a full understanding of layout properties in Android components.

One thing is I'm not sure the differences between these:

  • layout_margin vs. padding
  • layout_gravity vs. gravity vs. ignoreGravity

Should you use one over the other with Linear, Table or Relative Layouts? An example of something I'd like to learn is having an overall margin on a layout with separate components relating to the top/middle/bottom of the screen. The sdk docs are a good start, but they don't show how things work in different situations.

Any tips on where to go to learn a more complex/comprehensive layout design?

like image 672
wajiw Avatar asked Feb 17 '11 21:02

wajiw


1 Answers

Any attribute with the prefix layout_ is a LayoutParams attribute. While most view attributes are parsed during view construction by the view itself, LayoutParams are special arguments to the parent view that provide hints about how the parent should size and position the child view. Which LayoutParams are valid on a view depends entirely on the type of the parent view.

layout_margin is therefore an instruction to a parent view that supports margins. It says, "put this much space between me and other views or the edge of the parent." Padding is space inside a view between the view's edges and its content.

layout_gravity is gravity for a single child within its parent. gravity affects the contents of the view it appears on.

Which one you use depends on the result you want to achieve. If you want a layout to have a fixed amount of space between its edges and all of its contents, you want padding. If you want to move the layout's own edges in by a certain distance, you want margins. When you have layouts without backgrounds set, these two can be visually equivalent. When you start creating complex UIs where layouts have 9-patch backgrounds that visually group the contents the differences become apparent.

like image 78
adamp Avatar answered Oct 06 '22 23:10

adamp