Android custom table controls meet people’s visual needs

The Android platform has provided us with many standard components, such as textview, editview, button, ImageView, menu, etc., as well as many layout controls, such as absolutelayout, linerlayout, relativelayout, tablelayout, etc. However, with people's demand for vision, the basic components can no longer meet people's requirements for innovation and difference, so we often customize components to achieve a more beautiful UI interface.

There are usually two ways to implement custom controls. One is to inherit the view class and override important methods. The other is to inherit the ViewGroup class and redraw components by overriding some methods in the parent class. Recently, I did an exercise of customizing table controls, from which I summarized some experience. In this exercise, I redrawn the container component used to render the table style by inheriting the ViewGroup class. First, let's take a look at the parent class ViewGroup. This class has three construction methods: ViewGroup (context context), ViewGroup (context, attributeset attrs), ViewGroup (context, attributeset attrs, int defstyle). Our custom class that inherits ViewGroup needs to implement at least one construction method. Several methods in ViewGroup are very important. These methods can better help us realize the layout and rendering of our own components.

1. Onlayout method

This method is used to place the child controls in the container. If you do not override this method, the child controls will not be displayed in the layout controls. This method has five parameters to set the positions of the upper, lower, left, right and one flag bit of the child controls. This method must also be implemented by the subclass because it is an abstract method.

2. Addview method

This method is used to add child controls to container components

3. Dispatchdraw method

Through this method, we can obtain the canvas object, which allows us to draw any graphics we want on the component. In this table control, we can draw the outer border and table line of the table on the canvas

4. Getchildcount and getchildat methods

These two methods are used to obtain the number and location of child controls in the container control, so as to facilitate the layout and layout of child controls

5. Onmeasure method

This method is used to measure the size of the child control. It is called before the onlayout method to measure the size of the child control, and then draw the layout position of the child control in the container component

The following code example is given directly for reference only

The first is the class of the table control:

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