Custom combination control of Android custom control (3)

preface:

The first two articles introduce the basic principle of custom controls, and the detailed explanation of the basic principle of Android custom controls (I) and the custom properties of Android custom controls (II). Today, I will focus on how to improve layout reuse, reduce development cost and maintenance cost through custom composite controls.

What are the benefits of using custom composite controls? We often encounter many similar or identical layouts in project development, such as the title bar of app. We implement the title bar in three ways to compare the benefits of custom components. After all, good things are still oriented to improving development efficiency and reducing development costs. 1.) the first method: directly write the same title bar layout code in each XML layout

This method does not have any concept of layout reuse. At the same time, it also makes the current layout cumbersome, difficult to maintain and inefficient. Moreover, it also requires each developer to be careful, otherwise it may make uneven title bars. Therefore, this method is the least recommended.

2.) the second method: use the include tag to define the title block layout first

Then implement the reference through the include tag where necessary

Through the above layout code, we can use the above method to realize the reuse of layout, and avoid the problem that developers develop uneven title blocks. However, it also introduces new problems, such as further reducing the development efficiency and increasing the development cost. The problem is how to define title blocks for each layout file? The title problem, left and right buttons and other properties can only be set through code, resulting in high coupling between layout properties and activity code, so this method is not recommended.

3.) the third way: through the user-defined composite control, we will not specifically introduce how to implement a user-defined composite control, but the benefits brought by the user-defined composite control. • Improve the efficiency of layout file development • reduce the cost of layout file maintenance • reduce the coupling between layout file and activity code • easy to expand • easy to use

How to implement a custom composite control 1.) Define a layout file first

Note: why do you use the merge tag here? When customizing composite controls, you will inherit relativelayout, LinearLayout and other controls, which leads to an invisible increase in the level of layout. The comparison is as follows:

2.) define custom attributes, such as title text, button icon on the left of title bar, etc.

3.) customize a view and inherit different ViewGroup subclasses according to requirements, such as relativelayout, LinearLayout, etc. we inherit relativelayout here.

4.) reference in different XML layouts. I won't explain how to use custom attributes here. In order to view the effect more intuitively, I implement different title blocks in a layout file

Display effect

Summary:

Through this article, we know that custom composite controls can indeed improve development efficiency and reduce maintenance costs, but they also need to maintain a high degree of consistency in UI design style, otherwise we can only ha Da! Therefore, if you want to do an app well, you need a consensus team. This introduction ends here. I haven't thought about what to update next! It may be the event callback of a custom control, or it may be the streaming layout of a custom ViewGroup. Click view.

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