LinearLayout in Android

A LinearLayout is a layout that arranges its children either horizontally or vertically, depending on the value of its orientation property. The LinearLayout is the easiest layout to use. A LinearLayout can also have a gravity attribute that affects its gravity.

A horizontal LinearLayout Example

<LinearLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:orientation=“horizontal”
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<ImageButton
android:src=”@android:drawable/btn_star_big_on”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”/>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=”@string/hello_world” />
<Button android:text=“Button1”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”/>
</LinearLayout>

Vertical linear layout

<LinearLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:orientation=“vertical
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<ImageButton
android:src=”@android:drawable/btn_star_big_on”
android:layout_gravity=“center”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”/>
<TextView
android:layout_gravity=“center”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginLeft=“15dp”
android:text=”@string/hello_world”/>
<Button android:text=“Button1”
android:layout_gravity=“center”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”/>
</LinearLayout>

A LinearLayout can also have a gravity attribute that affects its gravity.

For example

A vertical LinearLayout whose gravity attribute is set to bottom.

<LinearLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:orientation=“vertical
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:gravity=“bottom>
<ImageButton

android:src=”@android:drawable/btn_star_big_on”
android:layout_gravity=“center”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”/>
<TextView
android:layout_gravity=“center”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginLeft=“15dp”
android:text=”@string/hello_world”/>
<Button android:text=“Button1”
android:layout_gravity=“center”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”/>
</LinearLayout>

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *