Method for realizing layout reuse of Android composite control

Many projects have the practice of implementing their own title bar. The usual interface is composed of buttons or text on the left, plus the title in the middle and buttons or text on the right. A better approach is to use the include tag to reuse the same XML file to reuse the layout. However, this method sets other properties such as title, left and right buttons through code, which will lead to high coupling between layout properties and activity code.

Therefore, we should customize the view and inherit the ViewGroup subclass to realize such a layout and reduce the coupling between the layout file and the activity code.

First, we need to write out the layout file layout_ custom_ titlebar.xml。

2. Define custom attributes

3. Customize a view to inherit the ViewGroup subclass. Here we inherit relativelayout.

4. Use it correctly

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