15. Layout Manager and ViewGroups
15.1. Available Layout Manager
A layout manager is a subclass of ViewGroup and is responsible for the layout of itself and its child Views. Android supports different default layout managers.
As of Android 4.0 the most relevant layout manager are LinearLayout, FrameLayout, RelativeLayout and GridLayout.
All layouts allow the developer to define attributes. Children can also define attributes which may be evaluated by their parent layout.
AbsoluteLayoutLayout is deprecated and TableLayout can be implemented more effectively via GridLayout
15.2. LinearLayout
LinearLayout puts all its child elements into a single column or row depending on the android:orientation attribute. Possible values for this attribute are horizontal and vertical, horizontal is the default value.
LinearLayout can be nested to achieve more complex layouts.
15.3. GridLayout
15.3.1. Overview
GridLayout was introduced with Android 4.0. This layout allows you to organize a view into a Grid. GridLayout separates its drawing area into: rows, columns, and cells.
You can specify how many columns you want for define for each View in which row and column it should be placed and how many columns and rows it should use. If not specified GridLayout uses defaults, e.g. one column, one row and the position of a View depends on the order of the declaration of the Views.
15.3.2. Tutorial GridLayout
Create the project "de.vogella.android.layout.gridlayout" with the Activity called "DemoGridLayout".
Change "main.xml" to the following.
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/GridLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent"