Working with Layouts

Most Android application user interfaces are defined using specially formatted XML files called layouts. Layout resource files are included in the / res/layout directory. You compile layout files into your application as you would any other resources.

Layout files often define an entire screen and are associated with a specific activity, but they need not be. Layout resources can also define part of a screen and can be included within another layout.

Did you Know?

Here is an example of a layout resource file:

<?xml version="i.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android android:orientation="vertical' 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" /> </LinearLayout>

You might recognize this layout: It is the default layout, called main.xml, created with any new Android application. This layout file describes the user interface of the only activity within the application. It contains a LinearLayout control that is used as a container for all other user interface controls—in this case, a single TextView control.

The main.xml layout file also references another resource: the string resource called @string/hello, which is defined in the strings.xml resource file.

DLKnsw?

Layouts can also be created, modified, and used at runtime. However, in most cases, using the XML layout files greatly improves code clarity and reuse.

There are two ways to format layout resources. The simplest way is to use the layout resource editor in Eclipse to design and preview layout files. You can also edit the XML layout files directly.

0 0

Post a comment