Android interface layout 2

Source: Internet
Author: User

Nowadays, Android is crazy, so the online Android learning materials have sprung up, and these basic things are even more powerful, I will record the difficulties I encountered and how to solve the problems I have used, so that I can review them for myself. If I encounter similar problems in the future, I can take a look. Second, it can help beginners a little bit.

In Android, we often use the following layout Methods: linearlayout (linear layout), relativelayout (relative layout), tablelayout (table layout), and absolutelayout (absolute layout ), framelayout (frame layout ).

 

Linearlayout and relativelayout are two of the most commonly used methods. Absolutelayout is rarely used (I have never used it myself), because it is based on the absolute position of the screen. If the screen size changes, the position of the control also changes. This is equivalent to the absolute layout in HTML, which is generally not recommended.

 

Linearlayout, as its name implies, is to arrange controls one by one. The linear layout is divided into horizontal and vertical linear attributes.

Android: Orientation ="Horizontal"Android: Orientation ="Vertical ".Xmlns: Android = "http://schemas.android.com/apk/res/android" Many people are confused about this, it is actually a namespace.

 

Relative layout of relativelayout. The relationship between each control is relative. If no relative relationship is set, it is displayed in the upper left corner of the screen by default. Important attributes are as follows:

Android: layout_torightof ="@ ID/City": Align with the right side of the control with the ID of city.

Android: layout_aligntop = "@ ID/City": align with the top of the control with the ID of city.

As follows:

Android: layout_width ="Fill_parent"Android: layout_height ="Wrap_content"I usually set relative layout in the linear layout. At this time, note that the width of the relative layout of the above two attributes can be set to fill the parent control, however, do not set the height to fill in the parent control. In this case, I want to put the control out of the relative layout without any effect because the screen has been occupied by the relative layout.

No matter what layout is used, the attributes of width and height must be clear. At the beginning, I often find that some controls are not displayed on the interface or occupy the entire screen, I checked that it was set incorrectly here. Also, do not forget that these two attributes are indispensable in general controls. If you forget to set them, an error will be reported.

 

The tablelayout table is not as flexible as the table in HTML. You can only use the tablerow attribute to control its rows, and the columns have several controls (generally ). The following code:

<Tablelayout>

<Tablerow>

<Edittext> </edittext>

<Edittext> </edittext>

</Tablerow>

<Tablerow>

<Edittext> </edittext>

<Edittext> </edittext>

</Tablerow>

</Tablelayout>

Table with two rows and two columns.

Android: gravity ="Center"The written explanation is the weight ratio. At that time, it is to center the display.

It can also dynamically add each column in each row. The following code is used:

 

/* Search for table objects by ID */

Tablelayout = (tablelayout) findviewbyid (R. Id.Table01);

/* Create a column object */

Tablerow =NewTablerow (This);

/* Text box object */

Textview temp =NewTextview (This);

Temp. settext ("text value ");

/* Add the text to the column */

Tablerow. addview (temp );

Android: stretchcolumns ="1, 2, 3, 4"It means to automatically stretch columns 1, 2, 3, and 4.

 

Framelayout frame layout. The controls in it are all displayed on the previous control according to one of the following controls. For example:

<Framelayout Android: layout_width ="Wrap_content"

Android: layout_height ="Wrap_content"Android: layout_weight ="1">

<Imageview Android: Id ="@ + ID/iv1"Android: layout_width ="Wrap_content"

Android: layout_height ="Wrap_content"Android: visibility ="Invisible"

Android: src ="@ Drawable/lotusleaf"> </Imageview>

<Imageview Android: Id ="@ + ID/F1"Android: layout_width ="Wrap_content"

Android: layout_height ="Wrap_content"Android: src ="@ Drawable/frog_right"

Android: visibility ="Invisible"> </Imageview>

</Framelayout>

The control with ID F1 is superimposed on the control with ID iv1. The interface layout of the previous games where frogs cross the river is "frog standing on the lotus leaf". This effect is achieved through this layout.

 

These layout methods are the most common for beginners to master, and the above is also a few very important and commonly used attributes. Many other attributes need to be tried to know the effect. I hope this blog will be helpful to beginners.

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.