Detailed explanation of common Android layouts (FrameLayout, LinearLayout, relativelayout)

As soon as many developers hear that the screen sizes of Android terminals are diverse and the screen resolutions are strange, they feel that Android development must be a headache in terms of screen adaptation. Before the advent of Android, the UI solutions known to most developers were roughly divided into two categories:

1. CSS in web development, layer by layer de cascading style. 2. Calculate the size of each uiview in IOS development.

Either of the above two solutions is a nightmare for the severely fragmented Android terminal. Fortunately, Android provides another solution to deal with serious terminal fragmentation, which is layout and 9-patch.

Here we want to talk about the layout. When the Android SDK was just released, Android provided five layouts: absolutelayout, FrameLayout, LinearLayout, relativelayout and tablelayout to deal with the problem of terminal fragmentation.

However, Android soon found that absolutelayout is a stupid scheme. This layout is no longer supported in Android 1.5 system. Although tablelayout is still supported in the remaining four layouts, due to the emergence of fragment and new tablayout, the blogger hereby asserts that tablelayout will not live long, and it will only be removed sooner or later.

Therefore, there are only three of the five basic layouts of Android (here is the basic layout, and the new layout introduced in the Android support package is not included). Let's introduce these three basic layouts respectively.

1、 FrameLayout FrameLayout should be the simplest layout in the Android system. By default, the elements in FrameLayout take the sitting vertex of the FrameLayout control as the reference point, overlapping layer by layer, and the added elements cover the previous elements.

Here's a demonstration. The code is as follows:

The operation results are as follows:

In the code, there are three views, and only two views, one black and one green, can be seen in the running results. This is because the red view is covered by the black view.

In FrameLayout, through Android: Layout_ The gravity attribute specifies the position of the child element. Next, adjust the position of the black view in the appeal example to display the red view. The adjusted code is as follows:

You can see that Android: layout is added to the above code_ Gravity attribute, and two values are specified, one is bottom and the other is right, indicating that the view will be placed in the lower right corner of the FrameLayout. The operation results are shown in the figure below:

2、 LinearLayout LinearLayout is a linear layout that allows the elements inside it to be arranged in a specified direction. The direction of the LinearLayout is specified by the Android: orientation attribute, and the way to it can be specified by the Android: gravity attribute.

Let's see the effect directly from the above code. The code is as follows:

In the code, the direction of LinearLayout is set to vertical, and its mode is aligned in the center, so the operation result is shown in the following figure:

In addition to setting Android: orientation to vertical, it can also be set to horizontal. Arrange the elements inside the LinearLayout horizontally, and change the Android: orientation attribute value in the above example to horizontal, as shown in the following figure:

3、 Relativelayout relativelayout is the most flexible and complex layout in the basic layout. Its internal elements can determine the layout by setting the relative relationship between them. When using relativelayout, it is recommended to set the ID for each internal element. The following is still a column to demonstrate the use of this layout. The code is as follows:

After analyzing the code, you can see that each view is set with an ID value, which is red, black, green, gray and orange. Then, through the code, we can see that black is on the right and below red, green is below black and right aligns its parent element (i.e. relativelayout), gray aligns its parent element (i.e. relativelayout) in the middle, orange is on the right of green and below gray, and the operation results are shown in the figure:

Here we summarize the layout related attributes in relativelayout:

android:layout_ Below: below the specified element Android: Layout_ Above: above the specified element Android: Layout_ Toleftof: to the left of the specified element Android: Layout_ Torightof: to the right of the specified element Android: Layout_ Centervertical: vertically center align parent element Android: Layout_ Centerhorizontal: align the parent element in the center horizontally Android: Layout_ Centerinparent: Center align parent element Android: Layout_ Align parentright: right align with parent element Android: Layout_ Alignparentleft: align left with parent element Android: Layout_ Align parenttop: align Android: layout with parent element_ Align parentbottom: align with the lower part of the parent element Android: Layout_ Align right: right align with the specified element Android: Layout_ Align left: align left with the specified element Android: Layout_ Aligntop: align with the specified element Android: Layout_ Align bottom: align with the specified element. Starting from Android 4.2, that is, API level 17, Android enhances relativelayout to better meet and localize this demand. For example, in some countries, text is read from right to left, which is called RTL. To cope with RTL, relativelayout adds the following attributes:

android:layout_ Align start: align with the start position of the specified element Android: Layout_ Tostartof: Android: layout at the beginning of the specified element_ Align parentstart: align with the parent element and the start side Android: Layout_ Alignend: align with the end and start position of the specified element Android: Layout_ Toendof: on the end side of the specified element Android: Layout_ Alignparentend: align with the end position of the specified element. We can understand the start and end here as follows:

Start: in countries with left to right reading habits, the start side is equal to the left, and the display effect of tostartof is equal to toleftof. However, in countries with right to left reading habits, the starting side becomes the right side, and the display effect of tostartof is equal to torightof. End: like the above understanding of the beginning, the end side is on the right side in countries with reading habits from left to right, and vice versa.

Original link: http://lyjbk.com/archives/158.html

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>