Android Study Notes 05: Layout

Source: Internet
Author: User

1. Layout Introduction

To adapt to various interface styles and meet development needs, Android provides five layout Methods: LinearLayout (linear layout), FrameLayout (Framework layout), and RelativeLayout (relative layout) absoluteLayout (absolute layout) and TableLayout (table layout). With these five la S, you can place various controls on the screen of your mobile phone as you like.

In the Android system, visualized controls are inherited from the Android. view. View class. You can create a widget in either of the following ways:

1. Configure various properties of the control in the xml layout file, and then load the control to be used in the program.

2. directly set the control properties in the program and load the control.

The xml layout file is a common method for defining controls in the android system. Its purpose and benefits are obvious. It isolates program code from the graphic UI to facilitate the maintenance of code and graphic UI.

The xml layout file must be included in the res/layout directory, and the root node of each xml layout file can be any view label. To specify an id for a tag in the xml file layout, use the following format:

Android: id = "@ + id/Tag name"

The id of each tag generates corresponding variables in the R class, and the tag name is saved in the R file.

To use an xml layout file, use setContentView in the oncreate method to load the specified xml layout file. The loading method is as follows:

Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );}

2. Common Layout attributes

In Android systems, there are usually three methods to indicate unit length: px, dp, and sp.

Px indicates the actual pixel of the screen. For example, a 320*480 screen means that the mobile phone screen has 320 pixels horizontally and 480 pixels vertically. Dp indicates the physical size of the screen, which is 1 inch of the size of 1/72. Sp is a pixel irrelevant to the scale. It is similar to dp, but can be scaled based on the user's font size preferences.

Generally, you can use dp or sp to specify the length, height, and other attributes. If you set the font, you need to use sp.

1. How to control the relative position of a control in the parent control?

Layout_margin is provided in the Android system to control the margin of a control edge relative to the parent control.

Android: layout_marginTop indicates the distance between a widget and its parent widget. Android: layout_marginLeft indicates the distance between a widget and Its left border. Android: layout_marginRight indicates the distance between a widget and Its right border. Android: layout_marginButtom indicates the distance between a widget and its parent widget.

2. How to control the relative position of content in a control?

Padding is provided in the Android system to control the margin of content in a control relative to the edge of the control.

Android: paddingTop indicates the distance between the content of a control and the top edge of the control. Android: paddingLeft indicates the distance between the content of a control and the left edge of the control. Android: paddingRight indicates the distance between the content of a control and the right edge of the control. Android: paddingButtom indicates the distance between the content of a control and the bottom of the control edge.

3. Differences between android: gravity and android: layout_gravity

Android: gravity is used to set the alignment of the View component, while android: layout_gravity is used to set the alignment of the Container component.

 

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.