Wednesday, May 16, 2012

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. RelativeLayout

RelativeLayout allow to position the widget relative to each other. This allows for complex layouts.
A simple usage for RelativeLayout is if you want to center a single component. Just add one component to the RelativeLayout and set the android:layout_centerInParent attribute to true.
    
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
         />

</RelativeLayout>
   

15.4. Gridlayout

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.5. ScrollView

ScrollViews can be used to contain one view that might be to big to fit on one screen. If the view is to big the ScrollView will display a scroll bar to scroll the context. Of course this view can be a layout which can then contain other elements.

No comments:

Post a Comment