Implementation example code of pop-up window at the bottom of Android

This article mainly introduces the correct posture of realizing the bottom pop-up in Android. If you encounter some problems in realizing the bottom pop-up, please read this article carefully. I believe this article will be helpful to you.

Harvest early know

After reading this article, you can have the following harvest

How to realize the bottom pop-up window

Due to my limited level, I only know several ways to realize the bottom pop-up window

Next, we will use the above three methods to realize the bottom pop-up window in Android.

Using popwindow to realize bottom pop-up window

Because this article mainly introduces the way to realize the pop-up window at the bottom, we will not explain popupwindow in detail. You can learn about popupwindow here.

Directly enter the topic, follow the routine, step by step, and use popupwindow to realize the bottom pop-up window. First, write a layout file as the contents of popupwindow. The layout file is as follows

Note: the method of filling the parent window is used here. If you don't do so, you can't see the effect of covering the back. It's easier to understand by looking at the following figure. The left figure is the method of filling the parent layout, and the right figure is

Adaptive approach

Note: because the parent layout is filled, all the pop-up windows here are popupwindow, so clicking the shadow pop-up window in the left figure will not disappear, because the shadow is also popupwindow! The solution is to fill the shaded part in the left figure with a textview control, and then set the click event for the textview. When you click the textview, just make the popupwindow disappear.

Let's take a look at the code of using popupwindow to realize the pop-up window at the bottom. I'll explain the important methods in detail

Focus on this code

This code sets where the pop-up window pops up. The void showatlocation (view parent, int gravity, int x, int y) method has four parameters. The first parameter is the parent layout, the second is where to pop up from the parent layout, and X and y are the offsets relative to the pop-up position of the parent layout. Since we want to place the mpopwindow at the bottom of the whole screen, we will use r.layout.activity_ As its parent container, main displays it in the bottom position.

Take a closer look at the figure above. The pop-up window at the bottom can't cover the status bar. Here's how to solve this problem.

Solve the problem that the pop-up window of popupwindow cannot overwrite the status bar

If you want to overwrite the status bar, you need to add the following code

Change the code that displays popupwindow again, as follows

Look at the effect again

Well, this is a perfect solution to the problem. You can find that using popupwindow to realize the bottom pop-up window is actually quite troublesome.

Using dialog to realize bottom pop-up window

First look at the code, and then explain it

Well, the code is a little long. In fact, it's easy to understand. Here we mainly talk about the content of oncreate method. You can take a closer look at the comments.

Using dialog to realize the bottom pop-up window is to inherit the system dialog, and then override the oncreate method to set the dialog to pop up from the bottom. Because it inherits dialog, it has the characteristics of dialog. If you touch the part other than the pop-up window at the bottom, the pop-up window will disappear automatically. There is no demonstration here. You can get the source code at the end of the text and experiment yourself.

Using dialogfragment to realize bottom pop-up window

Before implementing pop-up window, let's take a look at dialogfragment

Dialogfragment was introduced in Android 3.0. Is a special fragment used to display a modal dialog box on the content of the activity.

Using dialogfragment requires at least the oncreateview or oncreatedialog methods. Oncreateview displays the dialog using the defined XML layout file. Oncreatedialog creates a dialog using alertdialog or dialog. Next, the bottom pop-up window is realized by implementing the oncreateview method.

Because dialogfragment is also a fragment, dialogfragment has the same life cycle as fragment. Initialize the pop-up data in OnStart method and load the layout in oncreateview. Similarly, the use method of dialogfragment is the same as that of fragment. Let's see the use in activity

Conclusion

Well, the three ways to realize the bottom pop-up window have been discussed here. You can download the source code and study it. Here, it's good to choose the most suitable one when doing the project. Dialogfragment is recommended here. This way is highly customizable and the way to realize the pop-up window is also elegant.

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
分享
二维码
< <上一篇
下一篇>>