Layouts in Android
Common layout types used in Android are;
Ø
FrameLayout
Ø
LinearLayout
Ø
TableLayout
Ø
RelativeLayout
They all are subclasses of ViewGroup. The objects that are
placed inside these layouts are known as child UI elements. Let us see the
difference among these layouts in detail.
Ø FrameLayout
This is the simplest layout. The FrameLayout space can be
filled with only a single object. Therefore, if you place two objects, for
instance, a button and a textView, then one object will appear over the
previous one. If the new object is transparent, then the previous one would be
visible. Also, the items will appear only on the top left corner of the screen.
The location cannot be changed or the user is not allowed to set a location.
Example:
<?xml version="1.0"
encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"/>
</FrameLayout>
ScreenShot:

Ø LinearLayout
LinearLayout helps you in aligning the objects in a
particular direction. That is, either in vertical or horizontal direction. In
order to set this direction, we use the orientation attribute. So the child
elements or the objects placed in the LinearLayout are arranged one after
another like a stack manner.
Example for ‘vertical’ orientation
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"/>
</LinearLayout>
Example for ‘horizontal’ orientation
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"/>
</LinearLayout>
No comments:
Post a Comment