2 Layout
2 Layout
horizontal vercal
LinearLayout example 1
<LinearLayout ...
android:orientation="horizontal"
tools:context=".MainActivity">
<Button ... android:text="Button 1" />
<Button ... android:text="Button 2 Hooray" />
<Button ... android:text="Button 3" />
<Button ... android:text="Button 4
Very Long Text" />
</LinearLayout>
<LinearLayout ...
android:orientation="vertical"
android:gravity="center|right">
<Button ... android:text="Button 1" />
<Button ... android:text="Button 2 Hooray" />
<Button ... android:text="Button 3" />
<Button ... android:text="Button 4 Very Long Text" />
<Button ... android:text="Button 5"
android:layout_gravity="left" />
</LinearLayout>
Weight
● weight: gives elements relave sizes by integers
– widget with weight K gets K/total fracon of total size
– cooking analogy: "2 parts 6our, 1 part water, ..."
<LinearLayout ...
android:orientation="vertical">
<Button ... android:text="B1"
android:layout_weight="1" />
<Button ... android:text="B2"
android:layout_weight="3" />
<Button ... android:text="B3"
android:layout_weight="1" />
</LinearLayout>
Widget box model
● content: every widget or view has a certain size (width x height)
for its content, the widget itself
● padding: you can ar(cially increase the widget's size by
applying padding in the widget just outside its content
● border: outside the padding, a line around edge of widget
● margin: separaon from neighboring widgets on screen
Sizing an individual widget
● width and height of a widget can be:
– wrap_content : exactly large enough to (t the widget's content
– match_parent : as wide or tall as 100% of the screen or layout
– a speci(c (xed width such as 64dp (not usually recommended)
● dp = device pixels; dip = device-independent pixels; sp = scaling pixels
<Button ...
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Padding
● padding: extra space inside widget
– set padding to adjust all sides;
paddingTop, Boom, LeD, Right for one side
– usually set to speci(c values like 10dp
(some widgets have a default value ~16dp)
<LinearLayout ...
android:orientation="vertical">
<Button ... android:text="Button 1"
android:padding="50dp" />
<Button ... android:text="Button 2 Hooray" />
<Button ... android:text="Button 3"
android:paddingLeft="30dp"
android:paddingBottom="40dp" />
</LinearLayout>
Margin
● margin: extra space outside widget to separate it from others
– set layout_margin to adjust all sides;
layout_marginTop, Boom, LeD, Right
– usually set to speci(c values like 10dp
(set defaults in res/values/dimens.xml)
<LinearLayout ...
android:orientation="vertical">
<Button ... android:text="Button 1"
android:layout_margin="50dp" />
<Button ... android:text="Button 2 Hooray" />
<Button ... android:text="Button 3"
android:layout_marginLeft="30dp"
android:layout_marginTop="40dp" />
</LinearLayout>
GridLayout
● lays out widgets/views in lines of rows and columns
– orientaon aribute de(nes row-major or column-major order
– introduced in Android 4; replaces older TableLayout
● by default, rows and columns are equal in size
– each widget is placed into "next" available row/column index unless it is
given an explicit layout_row and layout_column aribute
<InnerLayoutType ...>
<Widget ... />
<Widget ... />
</InnerLayoutType>
<InnerLayoutType ...>
<Widget ... />
<Widget ... />
</InnerLayoutType>