Summary of the implementation of custom view in Android

What is Android custom view

In our daily development, many times the view provided by the system can not meet our needs. For example, we want to add a clear button to an edit text, and so on. At this time, we need to expand or combine the system view, which is the so-called user-defined view.

Types of Android custom views

Custom views can be divided into four categories, mainly through implementation

1. Self drawn control, inherit the view, override the OnDraw method, and draw in it. You need to adapt the margin, etc

2. Inheriting the special layout derived from ViewGroup, it is mainly used to realize custom layout, and also needs to adapt the margins, etc

3. Inherit a specific view (such as textview, etc.) without self adaptation. Support wrap_content and match_parent. You can add new functions to it

4. Inherit a specific ViewGroup, such as LinearLayout, which is mostly used for the combined view of multiple controls, and you don't have to adapt yourself

Self drawing control

This custom view is the most complex one, because it is necessary to adapt wrap_ conten,match_ The parent also draws different contents on the screen through conditional judgment, mainly rewriting the OnDraw method

The following is a simple OnDraw rewrite code

For the full version of this example, please check out another article and click here

Inherits the special layout derived from ViewGroup

It is mainly realized by loading a specific layout in the method and specifying the behavior of each view inside it.

Inherit specific views (such as textview)

You can add the response of a specific view to a specific event

Inherits the view of the specified ViewGroup

It is also implemented by loading a specific layout and then processing the view behavior in it. Most custom views that inherit ViewGroup can be implemented in this way, but the way of ViewGroup is closer to the bottom.

A simple example

summary

The above is the whole content of this article. I hope the content of this article can bring some help to Android developers. If you have any questions, you can leave a message. Thank you for your support for 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
分享
二维码
< <上一篇
下一篇>>