Android user interface design: Layout basics

Source: Internet
Author: User
Keywords Layout can XML

Understanding the layout is very important for good http://www.aliyun.com/zixun/aggregation/1997.html ">android application design." In this tutorial, we provide an overview of how layouts fit into the Android application architecture. We also explored some of the specific layout controls available to organize application screen content in a variety of ways.

What is layout?

Android developers use the term "layout" to refer to one of two meanings. Both definitions are used in this tutorial, and unfortunately they are mixed in the Android development community. The two definitions of layout are as follows:

A resource that defines what is drawn on the screen. Layout resources are stored in an XML file in the application's/res/layout resource directory. The layout resource is simply a template for the user interface screen, or part of the screen, and the content. A view class that primarily organizes other controls. These layout classes (Linearlayout,,relativelayout,tablelayout, and so on) are used to display child controls on the screen, such as text controls or buttons or pictures.

The Android user interface can be defined as a layout resource in XML or dynamically created by a program.

use Eclipse to design layout resources

Eclipse's Android Development plug-in includes a handy layout resource designer for designing and previewing layout resources. This tool includes two tab views: Layout view allows you to preview how the control will be displayed under different screens and for each direction; XML view tells you the XML definition of the resource. The layout Resource Designer looks like this:

Here are some tips for using the Layout resource Editor in Eclipse:

Use the overview (Outline) pane to add and remove controls in your layout resources. Select a specific control (in the Preview or Outline window) and use the Properties pane to adjust the properties of a particular control. Use XML tags to edit XML definitions directly.

It is important to keep in mind that the Eclipse Layout Resource Editor does not accurately simulate the presentation of layouts to end users. In this respect, you must test in an appropriately configured simulator, and more importantly, test on the target device. And some "complex" controls, including labels or video viewers, cannot be previewed in eclipse.

defines an XML layout resource

The most convenient and maintainable way to design a program's user interface is to create an XML layout resource. This approach greatly simplifies the UI design process by moving the static products and layouts of many user interface controls, as well as the definition of control properties in the mobile XML, instead of writing code. It adapts to the potential differences between UI designers (more concerned with layout) and developers (understanding Java and Implementing application functionality). Developers can still change screen content dynamically when necessary. Complex controls, such as ListView or GridView, typically process data dynamically with a program.

The XML layout resource must be stored under the/res/layout of the project directory. Creating an XML layout resource for each screen (closely associated with an activity) is a common practice, but it is not necessary. Theoretically, you can create an XML layout resource and use it in different activities to provide different data for the screen. If necessary, you can also distribute your layout resources and include them in another file.

Here is a simple XML layout resource, a linearlayout template that contains a textview and a imageview, defined in XML:

<?xml version= "1.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 " android:gravity= "center" > <textviewandroid:layout_width= "fill_parent" android:id= "@+id/photolabel" Android: layout_height= "Wrap_content" android:text= "@string/my_text_label" android:gravity= "Center_horizontal" Android: Textsize= "20DP"/><imageview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:src= "@drawable/matterhorn" android:adjustviewbounds= "true" android:scaletype= "Fitxy" android:maxheight= " 250DP "android:maxwidth=" 250DP "android:id=" @+id/photo "/> </LinearLayout>

This layout resource indicates that the screen contains two controls: first there is some text, and then there is a picture underneath it. These controls are organized in a vertically oriented linearlayout. The following two pictures show the possible styles for this layout on both horizontal and vertical screens:

Displaying a layout resource on the screen requires only one line of code including OnCreate () to be done. If the layout resources are stored in the/res/layout/main.xml file, the code may be:

Setcontentview (R.layout.main); dynamically define the layout with the program

You can also use programs to create user interface components. For ease of organization and maintainability, this is done only at special times, not in general. Instead of loading layout resources directly using the Setcontentview () method, you must create the screen content and then provide the Setcontentview () method with the parent layout object that contains all the child control content that you want to display.

For example, the following code shows how to instantiate a LinearLayout view with a program and place two TextView inside. No resources are used.

public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);//Setcontentview (R.layout.main ); TextView label = new TextView (this); Label.settext (R.string.my_text_label); Label.settextsize (20); Label.setgravity (gravity.center_horizontal); ImageView pic = new ImageView (this); Pic.setimageresource (R.drawable.matterhorn); Pic.setlayoutparams (New Layoutparams (Layoutparams.wrap_content, layoutparams.wrap_content)); Pic.setadjustviewbounds (TRUE); Pic.setscaletype (SCALETYPE.FIT_XY); Pic.setmaxheight (250); Pic.setmaxwidth (250); LinearLayout ll = new LinearLayout (this); Ll.setorientation (linearlayout.vertical); Ll.setlayoutparams (New Layoutparams (Layoutparams.fill_parent, layoutparams.fill_parent)); Ll.setgravity (Gravity.center); Ll.addview (label); Ll.addview (pic); Setcontentview (LL);

As you can see, the size of the code increases rapidly as more controls are added to the screen, making the screen content more difficult to maintain and reuse.

explore different layout types

Now let's turn our attention to layout controls that are useful for other controls in the organization. The most commonly used layout classes are:

framelayout– is used to display a bunch of child view controls. Multiple view controls can be added to this layout. It can be used to show multiple controls in the same screen space. linearlayout– is used to display child view controls in a single or single column. This is a convenient layout for creating a form. relativelayout– are used to display child view controls relative to each other. For example, you can set a control relative to another control "above" or "below" or "on the left" or "on the right". You can also place child view controls relative to the bounds of the parent element. tablelayout– is used to organize child view controls into rows or columns. For each row of the table, a single view control is added to each row of the table using the TableRow layout view.

uses a composite layout to organize controls

Layout (linearlayout,tablelayout,relativelayout, etc.) is also a control like other controls. This means that the layout control can be nested. For example, to organize the controls on the screen you can use a relativelayout in a linearlayout, and vice versa. The following figure shows a screen that has a linearlayout (parent), a tablelayout (top child node), and a framelayout (the bottom child node).

But be careful! Make sure your screen is relatively simple, complex layouts are slow to load and can cause performance problems.

provides optional layout resources

Consider device differences when you design your program layout resources. Usually it is possible to design a flexible layout that looks good on a variety of devices, whether it is a vertical screen or a mode screen. When necessary, you can introduce optional layout resources to handle special situations. For example, you can provide a different layout for loading depending on the direction of the device or whether the device has an oversized screen, such as a network tablet.

For more information on how to use optional resources, see the Android SDK documentation for Android resources.

Layout Tools and optimizations

The Android SDK includes several tools to help us design, debug, and optimize layout resources. In addition to the layout Resource designer built into Eclipse's Android plug-in, you can use the Hierarchy Viewer (Hierarchy Viewer) and layoutopt provided by the Android SDK. These tools can be found in the/tools directory of your Android SDK.

You can use the hierarchy viewer to see the details of the layout at run time. Find out more about the Hierarchy Viewer section of the Android developer website.

You can use the Layoutopt (Layout optimization) command-line tool to optimize your layout files. Optimizing the layout is important because complex layout files are loaded slowly. The Layoutopt tool simply scans the XML layout file and finds unnecessary controls. Check out the layoutopt section of the Android Developer website for more information.

Summary

The android application user interface is defined using a layout. There are many different types of layout types that you can use to organize controls on your screen. The layout can be defined using an XML resource, or it can be defined at run time by a Java program. Optional layouts can be loaded under special circumstances, such as providing an optional user interface in both horizontal and vertical screens. Finally, a well-designed layout is important for application performance, using the Android SDK tools like hierarchy Viewer and layoutopt to debug and optimize your application layout. [中文版]

Reprint Please specify:
Author: rockux–web Front end
From: Android user interface design: Layout basics

original hard, please support!
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.