Вы находитесь на странице: 1из 22

Android development

A N D R O I D
L A Y O U T S


Relative Layout
In RelativeLayout, each child element is laid
out in relation to other child elements; that is,
the location of a child element is specified in
terms of the desired distance from the existing
children
For instance, we can set a child View to be
positioned above or below or to the left
of or to the right of another View, referred
to by its unique identifier.
Attributes Of Relative
Layout
The attributes used to set the location of the control relative to a
container are :
android:layout_alignParentTopThe top of the control is set to align with
the top of the container.
android:layout_alignParentBottomThe bottom of the control is set to
align with the bottom of the container.
android:layout_alignParentLeftThe left side of the control is set to align
with the left side of the container.
android:layout_alignParentRightThe right side of the control is set to
align with the right side of the container.
android:layout_centerHorizontalThe control is placed horizontally at
the center of the container.
android:layout_centerVerticalThe control is placed vertically at the
center of the container.
android:layout_centerInParentThe control is placed horizontally and
vertically at the center of the container.

Attributes Of Relative
Layout
The attributes to control the position of a control
in relation to other controls are
android:layout_aboveThe control is placed
above the referenced control.
android:layout_belowThe control is placed
below the referenced control.
android:layout_toLeftOfThe control is placed
to the left of the referenced control.
android:layout_toRightOfThe control is placed
to the right of the referenced control.

Attributes Of Relative
Layout
The attributes that control the alignment of a control in
relation to other controls are
android:layout_alignTopThe top of the control is set to
align with the top of the referenced control.
android:layout_alignBottomThe bottom of the control is
set to align with the bottom of the referenced control.
android:layout_alignLeftThe left side of the control is
set to align with the left side of the referenced control.
android:layout_alignRightThe right side of the control is
set to align with the right side of the referenced control.

Attributes Of Relative
Layout
For spacing, Android defines two attributes:
android:layout_margin and android:padding.
The android:layout_margin attribute defines
spacing for the container, while android:padding
defines the spacing for the view.
Attributes Of Relative
Layout
The attributes that define the padding i.e. spacing between the
control and its contents are:
android:paddingDefines the spacing of the content on all four
sides of the control. To define padding for each side individually, use
android:paddingLeft, android:paddingRight, android:paddingTop,
and android:paddingBottom.
android:paddingTopDefines the spacing between the content
and the top of the control.
android:paddingBottomDefines the spacing between the content
and the bottom of the control.
android:paddingLeftDefines the spacing between the content
and the left side of the control.
android:paddingRightDefines the spacing between the content
and the right side of the control.

Attributes Of Relative
Layout
The attributes that define the spacing between the control and the
container are:
android:layout_marginDefines the spacing of the control in
relation to the controls or the container on all four sides.
android:layout_marginTopDefines the spacing between the top
of the control and the related control or container.
android:layout_marginBottomDefines the spacing between the
bottom of the control and the related control or container.
android:layout_marginRightDefines the spacing between the
right side of the control and the related control or container.
android:layout_marginLeftDefines the spacing between the left
side of the control and the related control or container.



SOME
EXAMPLES
EXAMPLE 1
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<Button
android:id="@+id/Apple"
android:text="Apple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:layout_marginLeft="20dip" />

EXAMPLE 1
<Button
android:id="@+id/Mango"
android:text="Mango"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="28dip"
android:layout_toRightOf="@id/Apple"
android:layout_marginLeft="15dip"
android:layout_marginRight="10dip"
/>

</RelativeLayout>
THE OUTPUT



EXAMPLE 2
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<Button
android:id="@+id/Apple"
android:text="Apple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:layout_marginLeft="20dip" />

EXAMPLE 2
<Button
android:id="@+id/Mango"
android:text="Mango"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="28dip"
android:layout_toRightOf="@id/Apple"
android:layout_marginLeft="15dip"
android:layout_marginRight="10dip"
/>
<Button
android:id="@+id/Banana"
android:text="Banana"
android:layout_width="200dip"
android:layout_height="50dip"
android:layout_marginTop="15dip"
android:layout_below="@id/Apple"
android:layout_alignParentLeft="true"
/>


EXAMPLE 2

<Button
android:id="@+id/Grapes"
android:text="Grapes"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:minWidth="100dp"
android:layout_alignParentRight="true"
android:layout_below="@id/Banana" />
<Button
android:id="@+id/Kiwi"
android:text="Kiwi"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_below="@id/Banana"
android:paddingTop="15dip"
android:paddingLeft="25dip"
android:paddingRight="25dip" />

</RelativeLayout>
THE OUTPUT



EXAMPLE 3
What changes will you make in
the previous code so that the
text Grapes appears at the
center top of the Button?


THE OUTPUT



EXAMPLE 3 Soln
We can make the text Grapes appear centrally at the top
row by adding the following line:
android:gravity="center_horizontal"
So, its tag appears as follows:
<Button
android:id="@+id/Grapes"
android:text="Grapes"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:minWidth="100dp"
android:layout_alignParentRight="true"
android:layout_below="@id/Banana"
android:gravity="center_horizontal" />



EXERCISE
Question
Create a simple Login Form application that asks
the user to enter a User ID and Password. The
TextView, EditText, and Button controls in the
application should be laid out in a RelativeLayout
container .Following behaviour is reqd:
1. If either the User ID or Password is left blank,
the message The User ID or password is left
blank. Please Try Again is displayed.
2. If the correct User ID and Password, are
entered, then a welcome message is displayed.
3. Otherwise, the message The User ID or
password is incorrect. Please Try Again is
displayed.
THE REQD OUTPUT

Вам также может понравиться