Examples to explain Android custom controls
Xiaobian introduced the usage of Android custom controls to you before. You can refer to the following:
Detailed usage of custom controls developed by Android
Explain Android custom control properties
You can see that the toolbar on QQ is actually a custom view. You can see that different interfaces simply modify the text. It is particularly obvious in the second and third photos. We set a custom control to imitate the toolbar of QQ
Before we start, let's first understand how the official implements a control, such as a LinearLayout
It doesn't have a layout_ Width and layout_ Are these two attributes height? In fact, these two attributes are officially defined
The official operation actually inherits a ViewGroup. LinearLayout is equivalent to a user-defined view. We don't need to delve into the methods. When we open the path of path, we can find an atts.xml file. When we open it, we can see the properties about layout that define LinearLayout, including layout_ Width and layout_ We can see the height attributes here, and we can see that we refer to the attributes to be used in the layout file
Starting with the text, we will explain the steps of a custom control:
1. Properties to be used in design
First switch the mode to project mode
Create a new moudle
Modify related package name and library name
Select the first one here, that is, do not create the activity interface, and then click finish
Expand topbar - > res - > values, and create an atts.xml file in values
The XML file code is as follows:
See this article for a deeper understanding http://blog.csdn.net/zjh_ 1110120/article/details/50976027
2. Implement a control we need
In the Java folder of topbar, create a new topbar class to inherit relativelayout
Then, rewrite the constructor with two parameters. The attributeset class is used to store various attributes previously defined in the XML file
Then, create an object TA of typedarray class and call the obtainstyleattribute method of context to obtain various attributes in atts.xml we just created. The second parameter topbar is < declare styleable name = "topbar" > defined in atts.xml above
After that, you can use the getxx method in the typedarray class to obtain the relevant attributes defined in XML. The attributes defined in topbar and atts.xml are connected with underscores
You can see that some of the second parameters are 0, which means that if the property value of the control is not set in the layout file, it will be 0 by default
Also remember to recycle in the end
Then get the previous property value to set the relevant properties
Here, when we use the properties of this custom control in the layout file, this class gets the value of the attribute we entered, and then calls the above method to set up.
After this step, we also need to put the left and right buttons and the title textview in a view, and add them to view using the addview method of layoutpram
First declare three layoutprams
After setting the width and height, use addview to add it to the view
At this step, the layout is completed, but the click events of the left and right buttons need to be realized. Setting a click listener directly is completed
However, this is not the template we want. If the template is used, we should directly set the listener in the activity. It is not faster, and we should not write relevant event processing in the control. Therefore, we have to use the interface callback mechanism
Following the method of button setting listener, we can define an interface class topbarclicklistener, in which we declare two methods, corresponding to the click events of the left button and the right button. Then, we declare an interface class object listener, create a setontopbarclicklistener method, and pass the parameters to the previously declared listener
After that, modify the click events of the left and right buttons to call the methods in the interface. This is done
3、 Reference our controls
A reference is a reference like a button to assign values to attributes, just as we use attributes of buttons, textview and other controls
Here we can see that there is a cus, which is similar to the Android we use, such as the system defined button and other controls. We need to write Android in its properties. There is an xmlns: Android before= http://schemas.android.com/apk/res/android
This is the description of the control properties of Android itself. Android is equivalent to a name. We can write it casually. Later, we change the latter to res auto. Later, we use our custom properties to prefix the name we wrote before, just as in my figure
Cus can be specified arbitrarily, but it cannot be the same as the system namespace. Res auto is used in Android studio as above, and the complete package name is required after res in eclipse
You can also see the actual effect in the preview on the right
If you have any experience after learning, you can leave us a message directly below.