Detailed explanation of Android programming dynamic loading layout example [with demo source code]

This paper describes the method of dynamically loading layout in Android programming. Share with you for your reference, as follows:

Due to the needs of the project some time ago, you need to load different layout pages on one page according to different buttons. At that time, you thought of using tabhot. However, the interface diagram provided by artists can't use tabhot at all, so we think of a dynamic loading method to solve this requirement. Here I sorted it out and wrote a demo. I hope you will take fewer detours in the future.

First, let's draw the frame of the interface, as shown below:

The white department in the middle is a linear layout file. I like to mark a layout with different colors when drawing for easy viewing. The layout file code is as follows:

As can be seen from the above rendering, the white linear layout is used to dynamically load the transmitted layout file. Well, let's do it. If we load the layout file dynamically. Let's achieve this effect step by step. First, outline the required XML, which is divided into the following steps.

Create a new layout to store the listview page. The code is as follows:

Create a new listview style for each row of data. The code is as follows:

Create another page to distinguish that this page is dynamically loaded. The code is as follows:

To add data to the listview, how to fill each row of data in the listview is not described in detail here. If you are puzzled, you can view the previous listview related articles. The code is as follows:

The project outline is as follows:

Well, now that our preparations have been completed, the next step is to teach you how to dynamically load the layout page drawn above. Let's take a look at the effect drawing first:

Click the first button

Click the second button

The dynamic loading code is as follows:

In the above, you can read the layout file every time you click the button by using the layouteinflator, and then find each view in the layout file. After the view is operated, it is loaded into the layout file to be placed in our setcontentview. Each time the file is dynamically loaded, you must call the removeallviews method to clear the previously loaded views. Isn't it simple? Of course, there are many ways to dynamically load view. Try different writing methods. You may understand different experiences. I wish you master Android development technology in the morning.

Tip: because the operation is based on view, you can use the animation effect to make the replacement interface more natural and more ornamental.

Click here to download the complete instance source code.

For more Android related content, readers who are interested can view the special topics of this site: summary of Android layout skills, introduction and advanced tutorial of Android development, summary of Android debugging skills and solutions to common problems, summary of Android multimedia operation skills (audio, video, recording, etc.), summary of Android basic component usage Android view skills summary and Android control usage summary

I hope this article will help you in Android programming.

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