Android Layout (2)

Source: Internet
Author: User

1. The name of an element XML element corresponds to a Java class, so a <TextView> element creates a TextView in your UI, a <linearLayout> element
Create a LinearLayout view group. When you load a layout resource, the Android system initializes these runtime objects, corresponding to the elements in your layout. Xml
The attributes of the element correspond to a method of a Java class.

2. ID attribute

Each view object has an associated ID that uniquely identifies it. This ID is referenced as an integer when the application is compiled. But the ID is usually in the layout XML text
As a string, as the id attribute of the element. This XML property is available for all view objects and is often used. The ID syntax in XML is as follows:

Android:id= "@+id/my_button"

The @ symbol before the string indicates that the XML parser should parse and extend the remaining ID string and use it as an ID resource. The + symbol indicates that this is a new resource name and it must be
is created and added to our resources (R.java file, R is Resource). The Android Framework provides some additional ID resources. When referencing an Android resource ID,
You do not need the + symbol, but you must add the Android package namespace as follows:

Android:id= "@android: Id/empty"

In order to create views and reference them from the application, the usual pattern is:

First, define a view/artifact object in the layout file and assign a unique ID:
<button android:id= "@+id/my_button"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/my_button_text"/>
Then create an instance of the View object and get it from the layout (typically in the OnCreate () method):
Button MyButton = (button) Findviewbyid (R.id.my_button);

3. Layout parameters

An XML layout property named Layout_something that defines the layout parameters that are appropriate for the viewgroup that it resides on. Each ViewGroup class implements an extended self-
The nested class of the viewgroup.layoutparams. This subclass contains the attribute types that define the size and location for each child view, to fit in the view group. As shown,
The parent view group defines layout parameters (including child view groups) for each child view.


Note that each layoutparams subclass has its own syntax for setting values. Each child element must define a layoutparams that is appropriate to its parent view, although it may also be
The sub-views define different layoutparams.

All view groups include broadband and height (layout_width and Layout_height), and each view requires that you define them. Many layoutparams also include optional
Margins and boundaries. You can specify a specific value for width and height, although you may not want to do so. More you will tell the view its size according to its content requirements or with the parent view
As large as the group allows (with wrap_content and fill_parent values, respectively).

4. Layout position & size & offset & margins

The geometric shape of the view is a rectangle. The position of the view is represented as a pair of left and top coordinates, and the dimensions (dimensions) are expressed as widths and heights. Units of position and dimensions
is a pixel (pixel).

You can retrieve the location of the view by calling GetLeft () and GetTop (). The former returns the left or X of the view rectangle coordinates, which returns the right or y of the view rectangle coordinates. These methods
Returns the relative position relative to its parent view, such as when GetLeft () returns 20, that is, the left distance of the view to its immediate parent view is 20 pixels.

In addition, some additional methods such as GetRight () and Getbuttom () are provided to avoid unnecessary computations. These methods return the right and bottom margins of the view coordinates. For example, calling
GetRight () is equivalent to the following calculation: GetLeft () +getwidth ().

The size of the view is also expressed as width and height, but is different from the above dimensions (dimensions). The dimensions above define how much the view wants to occupy in the parent view, the view's
Dimensions can be obtained by getmeasurewidth () and Getmeasureheight (). The size of the view represents the actual size of the view on the screen, and their values can be
It's not the same as the view size, but it doesn't have to be. The size of the view can be obtained by getwidth () and GetHeight ().

To measure the dimensions of a view, you must consider its padding (padding, the distance between the view content and the view border). The padding in pixels represents the left, top, right, and
Bottom part of the blank. The padding can be used to offset the view content by a specific number of pixels. For example, the left padding is 2 the output content is 2 pixels from the left border. The padding can be obtained by
The setpadding (int, int, int, int) method is set and queried by Getpaddingleft (), Getpaddingright (), Getpaddingtop (), Getpaddingbottom ().
。 Although a view can define padding, it does not support margins (margins). However, the view group supports margins.

Wrap_content and fill_parent difference, in fact, the difference from their word composition can be seen: wrap--package, parcel and so on content--content, then wrap_content table
The height/width of the display object is exactly the same as the content, fill--fills, etc., and parent--the parent, fill_parent means filling the parent view.

Note: units of layout position & size & padding in the above code (e.g. width= "250DP"). Units can be PX, in, MM, PT, DP, SP.

Px:pixels (pixels)--corresponds to the actual pixel on the screen
In:inches (Inch)--based on the size of the physical screen
Mm:millimeters (mm)--based on the size of the physical screen
Pt:points (dots)--inches of 1/72, based on the size of the physical screen
Dp:density-independent Pixels (pixel independent of density)-an abstract unit based on the density of the physical screen. These units are relative to a 160dpi screen
, all a DP is a point on the 160dpi screen. The conversion ratio of DP to PX varies by screen density, but is not necessarily proportional.
Sp:scale-independent pixels (scale independent pixels)-Similar to DP units, but it is also affected by user font size settings. When you specify a font size, use it
Because they will adjust according to the screen and user settings.

Android Layout (2)

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.