Android user interface design: linear layout

Source: Internet
Author: User
Keywords Layout this IDT we

Understanding layouts is important for good Android programming. In this tutorial, you'll learn all about linear layouts, which organize user interface controls or gadgets vertically or horizontally on the screen. With proper use, a linear layout can be used as a basic layout, based on which many interesting Android user interfaces can be designed.

what is a linear layout

Linear layouts are one of the simplest types of layouts that Android developers use, and developers use it to organize controls on your user interface. A linear layout acts like its name: it organizes the control in a vertical or horizontal form. When the layout orientation is set to vertical, all child controls inside it are organized in the same column, and when the layout orientation is set to horizontal, all child controls are organized on one line.

A linear layout can be defined in an XML layout resource file, or it can be dynamically defined in a program with Java code.

The following illustration shows a linear layout that contains 7 TextView controls. This linear layout direction is set to vertical, causing each TextView control to be displayed in a column. The Text property of each TextView control is a color value, and the background color is this colour; each control stretches to the screen width by setting the control's Layout_width property to Fill_parent.

defines a linear layout with 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 http://www.aliyun.com/zixun/aggregation/8760.html ">ui design process, which moves many of the static creation and the layout of user interface controls and the definition of control properties to XML. Instead of writing code.

The XML layout resource must be stored under the/res/layout of the project directory. Let's take a look at the rainbow Linear layout described in the previous section. This screen is basically a vertical linear layout that is set to fill the entire screen, which is implemented by setting its Layout_width and Layout_height properties as fill_parent. Appropriately named/res/layout/rainbow.xml,xml is defined as follows:

<?xml version= "1.0" encoding= "Utf-8" ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical " ><textview android:text= "RED" android:id= "@+id/textview01" android:layout_height= "Wrap_content" Android: Background= "#f00" android:layout_width= "fill_parent" android:layout_weight= "android:gravity=" Center "android: Textcolor= "#000" ></textview><textview android:text= "ORANGE" android:id= "@+id/textview02" Android: layout_height= "Wrap_content" android:layout_width= "fill_parent" android:layout_weight= "" android:background= " ffa500 "android:gravity=" center "android:textcolor=" #000 "></textview><textview android:text=" YELLOW " Android:id= "@+id/textview03" android:layout_height= "wrap_content" android:layout_width= "Fill_parent" Android: layout_weight= "android:background=" "#ffff00" android:gravity= "center" android:textcolor= "#000" ></ Textview>< TextView android:text= "GREEN" android:id= "@+id/textview04" android:layout_height= "Wrap_content" Android:layout_ Width= "Fill_parent" android:layout_weight= "android:background=" #0f0 "android:gravity=" Center "android: Textcolor= "#000" ></textview><textview android:text= "BLUE" android:id= "@+id/textview05" android:layout _height= "Wrap_content" android:layout_width= "fill_parent" android:layout_weight= "android:background=" "#00f" android:gravity= "center" android:textcolor= "#fff" ></textview><textview android:text= "INDIGO" Android: Id= "@+id/textview06" android:layout_height= "wrap_content" android:layout_width= "Fill_parent" Android:layout_ weight= "android:background=" "#4b0082" android:gravity= "center" android:textcolor= "#fff" ></TextView> <textview android:text= "VIOLET" android:id= "@+id/textview07" android:layout_height= "Wrap_content" Android: Layout_width= "Fill_parent" android:layout_weight= "android:background=" "#ee82ee" android:gravity= "center" Android:textcolor= "#000" ></TextView></LinearLayout>

You may notice that every child control in this linear layout has many interesting properties, including a property called Layout_weight. We'll talk more about it later.

The following two pictures show how this layout looks in the vertical and horizontal screen mode of the device:

Recall that in an activity, only one line of code with the OnCreate () method is needed to load and display a layout resource on the screen. If this layout resource is stored in the/res/layout/rainbow.xml file, this line of code should be:

Setcontentview (r.layout.rainbow), dynamic definition of linear layout with program

You can also use programs to create and configure linear layouts. This is done through the LinearLayout (Android.widget.LinearLayout) class. You can find child details in the Linearlayout.layoutparams class. Similarly, typical layout parameters (Android.view.ViewGroup.LayoutParams), such as Layout_height and Layout_width, and margin parameters (Viewgroup.marginlayoutparams) can also be used on LinearLayout objects.

Instead of loading layout resources directly by using the Setcontentview () method described earlier, you need to create screen content in Java and then provide a parent layout object to the Setcontentview () method that includes all the content of the control to be displayed as its child view. In this case, your parent layout object is a linear layout.

For example, the following code example illustrates how to instantiate a linear layout in an activity with a program and put three TextView objects into it in its OnCreate () method:

public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);//Setcontentview ( R.layout.rainbow); TextView TV1 = new TextView (this); Tv1.settext ("a"); Tv1.settextsize (m); Tv1.setgravity (Gravity.center); TextView TV2 = new TextView (this); Tv2.settextsize (m); Tv2.setgravity (Gravity.center); Tv2.settext ("Middle"); TextView TV3 = new TextView (this); Tv3.settextsize (m); Tv3.setgravity (Gravity.center); Tv3.settext ("Last"); 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 (TV1) ; Ll.addview (TV2); Ll.addview (TV3); Setcontentview (ll);

The following two pictures show how this layout looks under the device's vertical screen and horizontal screen:

Let's take a closer look at the Java code above. First, create and configure three TextView controls. Each control has a text size (font size), text alignment, and the literal value itself. Then, create a linear layout in a vertical direction. Its layout parameters allow it to populate the entire parent object (here is the entire screen), and its alignment causes all child controls to be arranged in the middle of the screen, not from the upper-left corner. Each TextView control is added to a linear layout using the AddView method as a child control. Finally, when the parent control is to be displayed on the screen, the linear layout is passed to the Setcontentview () method.

As you can see, as more and more controls are added to the screen, the amount of code grows quickly. For ease of organization and maintainability, it is best to use a program to define and use a layout in a special case rather than a general situation.

discussion on the important characteristics and properties of linear layouts

Now let's look at some important properties that help you configure the linear layout and its child controls.

Some special attributes apply to the linear layout. The most important attributes you will use for linear layouts include:

directional attributes (required), which can be vertical or horizontal (class: LinearLayout) alignment properties (optional), control how child controls are arranged and displayed in a linear layout (class: LinearLayout) layout_ The Weight property (optional, applied to each child control) specifies the relative importance of each child control in the parent's linear layout (class: Linearlayout.layoutparams)

In addition, the common Viewgroup-style properties are also applied to linear layouts. These properties include:

General layout parameters such as layout_height (must) and layout_width (must) (class: Viewgroup.layoutparams) margin layout parameters such as Margin_top,margin_left,margin_ Right and Margin_bottom (class: ViewGroup. Marginlayoutparams) layout parameters such as Layout_height and Layout_width (class: Viewgroup.layoutparams) Empower child controls

Most of the properties of linear layouts are self-evident. However, the Layout_weight attribute requires some additional discussion. Unlike other linear layout properties, other attributes apply to the linear layout view itself, which is applied to its child controls. The weight value itself should be a number (such as 0.5,0.25,0.10,0.10,0.05), if you add all the child control weights equal to 1 (100%).

The weight control of a child control is how "important" or how much "space" is left in the parent linear layout. This is best illustrated by examples. Let's go back to the rainbow linear layout we used before. To allow all child controls to "stretch" the linear layout identically, regardless of the size of the screen, we use layout_weight to give each textview a relative weight value. Because there are 7 colors we want to assign the same weight, we divide 1 by 7 to get 0.143. However, since we want the sum of the weights to be 1, the 5 control weights are set to 0.14, and the other two are 0.15--a tiny difference that makes the sum just as good as 1, But this is obvious for the first and last controls.

This weight technique works well when there is enough space on the screen to properly display all the controls. That is to say, when the space is very tight, the weight attribute may be covered by other factors, such as view cropping or trying not to wrap the text under TextView. This becomes apparent when we change the Rainbow.xml layout file to include a similar horizontal layout (layout_height or set to Fill_parent).

The following two pictures show the same layout (just changing to horizontal) in the device's vertical and horizontal screen mode:

We expect the red and purple regions (weights 0.15) to be slightly larger than the other colors (weights are 0.14), but that is not the case. If you look at the red TextView carefully, it should take up more space than the orange textview on the edge. However, because "Red" is a short word and "Orange" is not, the system automatically makes some collision adjustments in order to make each word not fold. The results are more pleasing, but there may be some annoyance if this is not the desired effect.

Summary

The Android user interface is defined using layouts, and linear layouts are one of the most basic types of layouts. Linear layouts allow child controls to be organized on a single line or row (horizontal) or a column (vertical). The position of the child control can be further set with alignment and weight properties. [中文版]

Reprint Please specify:
Author: rockux–web Front end
From: Android user interface design: linear layout

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.