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

When backing up files in Centos7, add the backup date to the backup file

Linux uses files as the basis to manage the devic...

New usage of watch and watchEffect in Vue 3

Table of contents 1. New usage of watch 1.1. Watc...

CSS3 achieves various border effects

Translucent border Result: Implementation code: &...

How to install openjdk in docker and run the jar package

Download image docker pull openjdk Creating a Dat...

Use of MySQL SHOW STATUS statement

To do MySQL performance adjustment and service st...

Tutorial on using portainer to connect to remote docker

Portainer is a lightweight docker environment man...

MySQL log trigger implementation code

SQL statement DROP TRIGGER IF EXISTS sys_menu_edi...

MySQL data aggregation and grouping

We often need to summarize data without actually ...

Super detailed tutorial to implement Vue bottom navigation bar TabBar

Table of contents Project Introduction: Project D...

Docker image access to local elasticsearch port operation

Using the image service deployed by docker stack,...

Example of adding multi-language function to Vue background management

Table of contents 1. First, configure the main.js...

Implementation of MYSQL (telephone number, ID card) data desensitization

1. Data desensitization explanation In daily deve...

Node quickly builds the backend implementation steps

1. First install node, express, express-generator...