Summary of three implementation methods of Android custom view

Custom controls are often used in the design completion project. I've always planned to summarize the implementation of custom controls. Let's summarize them today. Before that, I learned several blog posts about custom view on Guo Lin's great God blog. I feel I have benefited a lot. This article refers to some of them.

In summary, there are three ways to implement custom controls: composite controls, self drawn controls and inherited controls. These three methods will be introduced below.

(1) Composite control

Combined controls, as the name suggests, are the combination of some small controls to form a new control. Most of these small controls are built-in controls of the system. For example, the title bar control commonly used in many applications actually uses composite controls. Next, we will talk about the usage of composite controls by implementing a simple title bar custom control.

1. Create a new Android project and create a custom title bar layout file title_ bar.xml:

It can be seen that the title bar control is relatively simple. There is a return button on the left, and the background is a prepared picture back1_ 64.png, the title text is in the middle of the title bar.

2. Create a titleview class, which inherits from relativelayout:

In titleview, it mainly loads the layout for the customized title bar, adds an event listening method for the return button, and provides a method to set the title text.

3. In activity_ Introduce a custom title bar in main.xml:

4. Get a customized title bar in mainactivity and add a customized click event for the return button:

5. The operation effect is as follows:

In this way, the custom title bar is realized by combination. In fact, after more combination, more complex custom controls can be created, such as custom search bar.

(2) Self drawing control

The contents of self drawn controls are drawn by themselves, and the drawing is completed in the OnDraw method of view. Here is a simple counter. Each time you click it, the count value will be added by 1 and displayed.

1. Create counterview class, inherit from view, and implement onclicklistener interface:

2. In activity_ The custom layout is introduced into main.xml:

3. The operation effect is as follows:

(3) Inherit control

It is to inherit the existing control, create a new control, retain the characteristics of the inherited parent control, and introduce new features. The following describes the implementation of a custom listview that supports horizontal sliding to delete list items.

1. Create delete button layout delete_ Btn.xml, which is displayed after sliding the list items horizontally:

2. Create a customlistview class, inherit from listview, and implement ontouchlistener and ongesturelistener interfaces:

3. Define list item layout custom_ listview_ Item.xml, whose structure is very simple, contains only one textview:

4. Define the adapter class customlistviewadapter, which inherits from arrayadapter < string >:

5. In activity_ Introduce a custom listview in main.xml:

6. Initialize the list, set the list item deletion button and click the event in mainactivity:

7. The operation effect is as follows:

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