The use of FrameLayout in six layouts

The use of FrameLayout in six layouts

Preface

In the last issue, we explained LinearLayout to you. In this issue, we will explain the use of FrameLayout. Compared with other layouts, FrameLayout can be said to be the simplest one, and its scope of use is relatively small, but it is also one of the six major layouts in Android. You will still encounter it during the interview, so let's start learning quickly~

Introduction

In FrameLayout, this layout directly opens up a blank area on the screen. All views added to this layout are displayed in a stacked manner, and it will place these views in the upper left corner of this area by default. The first view added to the layout is displayed at the bottom, and the last one is placed at the top. The views of the previous layer cover the views of the next layer, so the layout is similar to a stack layout, which is why FrameLayout is less used.

1. Common attributes

There are only two common attributes of FrameLayout:

Two properties:

  • android:foreground: Set the foreground image of the frame layout container
  • android:foregroundGravity: Sets the position of the foreground image display

Foreground image: It is always at the top of the frame layout, directly facing the user, and is the image that will not be covered.

Some of you may be confused about this explanation, so let us use actual operations to let everyone intuitively feel what it means!

In the picture above, we can see that background, as the name implies, is to set the background. So what is the foreground? I believe that my friends already have some ideas. Let's continue to look down.

I believe that after reading the above picture, you have completely understood what the foreground image is. The same code just changes the background to the foreground. It can be clearly seen that HelloWord can still be seen in Figure 1, but in Figure 2, it has been blocked. We can understand it simply like this: foreground is actually the cover of a book, which covers all the contents of the book.

2. How does FrameLayout set the position of its subviews (such as TextView)?

Set the value through layout_gravity, and then set the position with layout_marginTop and layout_marginLeft.

For example, there are 4 TextViews: the first layer is the company, the second layer is the office, the third layer is the workstation, and the fourth layer is the programmer. If the position of TextView is not set, the four words will be displayed in the upper left corner by default, with the company at the bottom and the programmer at the top. To achieve the position effect of the four pictures in the figure, the corresponding settings are as follows:

(1) First layer company: android: layout_gravity="center"

(2) Second-layer office: android: layout_gravity = "center" android:layout_marginTop="-150dp" (Note: This is a negative number because android:layout_gravity = "center" means centering in the horizontal and vertical directions. The baseline is the midpoint of the view in the horizontal and vertical directions. Since the office is above the baseline (negative direction), it is a negative number)

(3) Third level: android:layout_gravity="top" android:layout_marginTop="40dp" android:layout_marginLeft="40dp' (Note: android:layout_gravity="top" means vertical top alignment, while horizontal alignment is left alignment by default)

(4) Fourth-level programmers: android:layout_gravity="center" android:layout_marginTop="80dp" (corresponding to the analysis of the second-level office, it is easy to understand that the programmers are below the baseline (positive direction), so the number is negative. And note: the baseline here is not the top of the screen, but the center line of the horizontal and vertical directions respectively)

The final effect is as follows:

Conclusion

This is the end of the introduction to FrameLayout. Some of you may think that today's article does not contain too much content, mainly because FrameLayout is rarely used in actual projects, so everyone just needs a brief understanding of it. but! ! ! It has been a week since we started learning together. I believe there are still a few students who have not created their first project yet, so I hope you will take action quickly and write all the wonderful content of this week into your first personal demo. Friends who have been following us in actual practice, I hope you can also review the previous articles. Confucius said: "Isn't it a pleasure to learn and practice it from time to time?" ~

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  How to use docker to deploy spring boot and connect to skywalking

>>:  Detailed explanation of two quick ways to write console.log in vscode

Recommend

Solve the problem of docker container exiting immediately after starting

Recently I was looking at how Docker allows conta...

The background color or image inside the div container grows as it grows

Copy code The code is as follows: height:auto !im...

Using CSS3 to create header animation effects

Netease Kanyouxi official website (http://kanyoux...

Implementation of Grid common layout

No gaps on both sides, gaps between each column w...

An IE crash bug

Copy code The code is as follows: <style type=...

Analysis of idea compiler vue indentation error problem scenario

Project scenario: When running the Vue project, t...

JavaScript implements password box verification information

This article example shares the specific code of ...

CSS3 realizes the mask barrage function

Recently I saw a barrage effect on B station call...

Use JavaScript to create page effects

11. Use JavaScript to create page effects 11.1 DO...

Example of using Docker to build an ELK log system

The following installations all use the ~/ direct...

JavaScript to implement a simple clock

This article example shares the specific code for...

Thoughts on copy_{to, from}_user() in the Linux kernel

Table of contents 1. What is copy_{to,from}_user(...

JavaScript Array Detailed Summary

Table of contents 1. Array Induction 1. Split a s...

How to create https using nginx and Tencent Cloud free certificate

I have been studying how to get https. Recently I...

How to install multiple mysql5.7.19 (tar.gz) files under Linux

For the beginner's first installation of MySQ...