Android xml Layout

Source: Internet
Author: User

First, perform a Rough Analysis Based on the program's directory structure:
Res/layout/This directory stores the xml file used for layout. The default value is main. xml.
Res/values/This directory stores a bunch of constant xml files.
Res/drawable/stores some images or something. Of course, the icons are also here.
The following describes the xml file in layout and summarizes the layout method:
· File start
<? Xml version = "1.0" encoding = "UTF-8"?>
This is to describe the xml version and character encoding.
· The following is the key part:
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/ android"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content">
</LinearLayout>
The "LinearLayout" at the beginning is the layout method, which can be divided into many types. The most common method is Linear, and other layout methods are summarized later.
Then, android: layout_width (height) = "wrap_content" indicates the width and height of the layout, or the absolute value. When the layout is set to the absolute value, the unit is marked.
· In <LinearLayout...> and </LinearLayout>. For example, you need to add a Button control named btn and the text displayed on the Button is "Test! ", Which can be written as follows:
<Button id = "@ + id/btn"
Android: text = "Test! "
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
The control name is followed by the id, which is used to add the event Listener. The android: xxx in the following lines sets the control attributes, these attributes are available in Android documents and do not need to be remembered. You can query them now.
· One thing to note is that the layout method can be nested, a bit like the Container in java, which can easily "Heap" the interface.
A brief description of the layout method:
I checked the Android document and found that there are indeed many la S, listing only two of which I think are commonly used:
· LinearLayout is a linear layout method, which is commonly used to add controls either up or down or left or right;
· The layout of a grid is translated from the GridView in Chinese. The control can be filled into each grid in order, and the interface displayed will be neat and commonly used;
  The specific la s are as follows: 
  @ <1>LinearLayout provides a model for horizontal and vertical control arrangement. You can also set the weight layout parameter of the Child control to control the relative size of each control in the layout. Horizontal (vertical) vertical (horizontal)
Fill-parent: full screen, wrap-content: the size of the control content.
Alignment gravity values:
Top: do not change the size and place it on the top of the container.
Bottom: The position is placed at the bottom of the container without changing the size.
Left: no size change. The position is placed on the left side of the container.
Right: no size changes. The position is placed on the right side of the container.
Center_vertical: The position is placed in the vertical center of the container without changing the size.
Center_horizontal: The position is placed in the center of the horizontal direction of the container without changing the size.
Center: without changing the size, the position is placed in the center of the horizontal and vertical direction of the container.
Fill_vertical: if possible, vertical extension can fill the container
Fiil_horizontal: if possible, the horizontal extension can fill up the container
Fiil: Fill the container vertically and horizontally if possible
 @ <2>AbsoluteLayout allows the child element to specify an accurate x/y coordinate value and display it on the screen. (0, 0) is the upper left corner. When you move down or to the right, the coordinate value increases. AbsoluteLayout has no page border and allows elements to overlap with each other (although not recommended ). We generally do not recommend AbsoluteLayout unless you have a legitimate reason to use it, because it makes the interface code too rigid, so that it can work well on different devices.
Android: layout_x/layout_y = "56px" to determine the control position
  @ <3>RelativeLayout (relative layout) allows child elements to specify their positions relative to other elements or parent elements (specified by ID ). Therefore, you can arrange two elements in the right-aligned, up/down, or in the center of the screen. Elements are arranged in order. Therefore, if the first element is in the center of the screen, other elements relative to the element are arranged in the relative position in the center of the screen. If you use XML to specify this layout, the associated elements must be defined before you define it.
Android: layout_centerInparent: place the current control in the horizontal and vertical central parts of the parent control. Android: layout_centerHorizontal places the current control in the central part of the parent control in the horizontal central part of the parent control.
Android: layout_centerVertival: place the current control in the center of the portrait of the parent Control
Android: layout_alignParentLeft: Align the left end of the current control with the left end of the parent Control
Android: layout_alignParentRight: Align the right end of the current control with the right end of the parent Control
Android: layout_alignParentTop: Align the top of the current control with the top of the parent Control
Android: layout_alignParentBottom: Align the bottom of the current control with the bottom of the parent Control
The preceding attribute can only be set to a Bool value, "true" or "false"
Android: layout_below/layout_abve/layout_toLeftOf/layout_toRightOf = "@ id/": place the current control below/above/left/right of the space with the given id
Android: layout_marginBottom/layout_marginLeft/layo ut_marginRight/layout_marginTop = "30px" to empty the corresponding pixel space at the bottom/left/right/top of the Current Control
  @ <4>FrameLayout is the simplest layout object. It is customized as a blank standby area on your screen, and then you can fill in a single object-for example, an image you want to publish. All child elements are fixed in the upper left corner of the screen. You cannot specify a position for a child element in FrameLayout. The next child element directly overwrites the previous child element and blocks them in part or whole (unless the last child element is transparent ).

Android: src = Platform
  @ <5>TableLayout (table layout) manages child controls in the form of columns. Each row is a TableRow object. You can also add child controls to TableRow.
Android: collapseColumns = "n" hides the TableRow column n in TableLayout.
Android: stretchColumns = "n": sets column n as an extended column.
Android: shrinkColumns = "n": Set column n to a column that can be shrunk.
Android: src = Platform

 

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.