Creation method of Android custom control

This article shares the specific code for Android to create custom controls for your reference. The specific contents are as follows

1. Imitating the style of iPhone, place a title bar at the top of the interface.

The title bar layout has been written, and the rest is how to use this title bar in the program.

Then hide the title bar of the system in mainactivity

We still need to write event registration code for these controls separately in each activity. For example, the return button in the title bar has the same function no matter which activity it is in, that is, it destroys the current activity. This situation is best solved by using custom controls.

Create a new custom title bar control:

We have rewritten the constructor with two parameters in LinearLayout, which will be called when the titlelayout control is introduced into the layout. Then, the title bar layout needs to be dynamically loaded in the constructor, which needs to be implemented with the help of layoutinflator. Through LayoutInflater's from () method, you can build a LayoutInflater object, then call inflate () method to dynamically load a layout file. The inflate () method receives two parameters. The first parameter is the ID of the layout file to load. Here we introduce R.layout.title, and the second parameter is to add a parent layout to the loaded layout. Here, we want to specify it as titlelayout, so we pass in this directly

Add this custom control to the layout file

Let's try to register the click event for the button in the title bar and modify the code in titlelayout

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