"Go" Android UI five layouts

Source: Internet
Author: User

In an Android application, layout is an important part of development, and layout is an integral component of the UI.

# # Android UI Core class There are several ways to build the UI in Android apps:
    1. Use Java code purely
    2. Using XML to fully define the interface
    3. Use both, defined in XML, referenced and modified in Java
The core class for the UI in the Android SDK:

Android.view.View and Android.view.ViewGroup

Common UI controls in Android extend View and ViewGroup part of it is designed to control its child view locations and sizes, which we call layout managers.

ViewGroup

1.LayoutParams is the viewgroup inner class that contains the layout parameters of the ViewGroup to tell their parent class how they want to lay out (size and position) in the parent class. All layout parameters defined in Layoutparams and its subclasses are defined in XML by layout_***.

The layoutparams provides only two parameter settings:

layout_width  元素的高度(fill_parent | match_parent | wrap_content | *dip)layout_height 元素的宽度(同上)                

2.MarginLayoutParams is also viewgroup an inner class that inherits the Layoutparams class, which extends the properties of the Layoutparams and sets the parameters.

Marginlayoutparams added four parameter settings:

layout_marginLeft       相对于本元素左边界的偏移layout_marginRight      相对于本元素右边界的偏移layout_marginTop,       相对于本元素上边界的偏移layout_marginBottom     相对于本元素下边界的偏移

The two layout parameters above, both sub-viewgroup and sub-view in all layouts can be used

5 layouts are described below:

# # 1.Linear layout (linear layouts)

The linear layout is the simplest layout in the Android layout, and is also the most commonly used, most practical layout.

Android:orientation alignment of the linear layout: vertical (vertical) and horizontal (horizontal)

Special parameters in the Layoutparams:

layout_weight  权值layout_gravity 相对于父元素的重力值(默认top|left):(top|bottom|left|right|center_vertical|fill_vertical |center_ horizontal |fill_ horizontal | center| fill)
The LinearLayout has two very similar properties:
android:gravityandroid:layout_gravity

The difference between them is:

    • The Android:gravity property is the qualification for the content in the view. For example, a button above the text. You can set the text relative to the view's left, right, and so on.

    • Android:layout_gravity is used to set the location of the view relative to the parent view. For example, a button in the LinearLayout, you want to put the button in the LinearLayout left, on the right and so on can be set by this property.

Android:gravity is used to set the alignment of content in view relative to the view component, while android:layout_gravity is used to set the alignment of the view component with respect to the container.

The principle is a bit similar to Android:paddingleft and Android:layout_marginleft. If both properties are set at the same time on the button.

android:paddingLeft="30px"  按钮上设置的内容离按钮左边边界30个像素android:layout_marginLeft="30px"  整个按钮离左边设置的内容30个像素

To get back to the point below, we can set the android:gravity= "center" to let the text in the EditText in the EditText Component Center display;

At the same time we set EditText's android:layout_gravity= "right" to let the EditText component display in LinearLayout. Look at the effect:

<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >    <EditText        android:layout_width="wrap_content"        android:gravity="center"        android:layout_height="wrap_content"        android:text="one"        android:layout_gravity="right"/></LinearLayout>
# # 2.Relative layout (relative layouts)

Relativelayout allows child elements to specify their location relative to other elements or parent elements (specified by ID). As a result, you can arrange two elements in the form of Right-aligned, or up or down, or centered on the screen. Elements are arranged sequentially, so if the first element is in the center of the screen, then the other elements relative to the element are arranged in the relative position of the center of the screen. If you use XML to specify this layout, the associated element must be defined before you can define it.

This layout is the most flexible layout, so we use this layout more complex layouts.

Layoutparams Special Parameters:< 1>. Property value is True or False
android:layout_centerHrizontal            水平居中android:layout_centerVertical             垂直居中android:layout_centerInparent             相对于父元素完全居中android:layout_alignParentBottom          贴紧父元素的下边缘android:layout_alignParentLeft            贴紧父元素的左边缘android:layout_alignParentRight           贴紧父元素的右边缘android:layout_alignParentTop             贴紧父元素的上边缘android:layout_alignWithParentIfMissing   若找不到兄弟元素以父元素做参照物
< 2>. The property value must be the reference name of the id "@id/id-name"
android:layout_below               在某元素的下方android:layout_above               在某元素的上方android:layout_toLeftOf            在某元素的左边android:layout_toRightOf           在某元素的右边android:layout_alignBaseLine       该控件的baseline和给定ID的控件的Baseline对齐android:layout_alignTop            本元素的上边缘和某元素的的上边缘对齐android:layout_alignLeft           本元素的左边缘和某元素的的左边缘对齐android:layout_alignBottom         本元素的下边缘和某元素的的下边缘对齐android:layout_alignRight          本元素的右边缘和某元素的的右边缘对齐
< 3>. The value of the property is a specific pixel value, such as 30dip,40px
android:layout_marginBottom        离某元素底边缘的距离android:layout_marginLeft          离某元素左边缘的距离android:layout_marginRight         离某元素右边缘的距离android:layout_marginTop           离某元素上边缘的距离
3.TableLayout (Table layout)

Tablelayout assigns the position of the child element to a row or column. A tablelayout is made up of many TableRow, and each TableRow defines a row (in fact, you can define other sub-objects).

The Tablelayout container does not display the row, cloumns, or cell border lines. Each row has 0 or more cells, and each cell has a view object.

A table consists of columns and rows that make up many cells. Table allows cells to be empty. Cells cannot span columns, which is not the same as in HTML.

Special parameters:

android:stretchColumns  伸展的列的索引android:shrinkColumns   收缩的列的索引android:collapseColumns 倒塌的列的索引(即销毁)

Represents a table of two columns in two rows. android:gravity= "Center" written explanation is weight ratio. It's really about showing it in the center.

It can also dynamically add each row inside each column. As shown in the following code:

TableLayout tableLayout = (TableLayout) findViewById(R.id.table01);TableRow tableRow = new TableRow(this);TextView temp = new TextView(this);temp.setText("text的值");tableRow.addView(temp);

android:stretchcolumns= "1,2,3,4″ it means to automatically stretch the 1,2,3,4 column.

# # 4.AbsoluteLayout (absolute layout)

Absolutelayout allows child elements to specify accurate x/Y coordinate values and display them on the screen. (0, 0) is the upper-left corner, and when you move down or to the right, the coordinate value becomes larger.

Absolutelayout does not have a page border, allowing elements to overlap each other (although not recommended). We generally do not recommend the use of

Absolutelayout, unless you have a legitimate reason to use it, because it makes the interface code too rigid that may not work well on different devices.

Special parameters in the Layoutparams:

layout_x   x方向的坐标layout_y   y方向的坐标
# # 5.FrameLayout (frame layout)

Framelayout is one of the simplest layout objects. It is customized as a blank alternate area on your screen, after which you can populate a single object. For example, a picture you want to publish. All child elements will be pinned to the upper-left corner of the screen; You cannot specify a location for a child element in Framelayout. The latter child element will overwrite the padding directly above the previous child element, blocking them partially or completely (unless the latter element is transparent).

You can put more than one control inside, but the position of the control is relative to the position

Special parameters in the Layoutparams:

Layout_gravity Gravity value relative to parent element (usage same as LinearLayout)

Precautions:

    1. Each layout should not use its own attributes. For example, the android:layout_x and android:layout_y belonging to the Absolutelayout layout are used for linearlayout layout or relativelayout layout, or the relativelayout layout Attributes such as Below,rightof are applied to other layouts. This does not make an error, but it does not achieve the effect we need.

    2. About Android:layout_width= "Fill_parent" and android:layout_height= "Wrap_content", this is the setting for each layout width and height. Wrap_content can indicate that the width or height of the layout changes depending on the control, similar to the auto-set width and height, fill_parent the layout to fill the entire screen, and another match_parent, which is essentially the same as fill_parent, And starting with API Level8 to replace Fill_parent.

"Go" Android UI five layouts

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.