The system control view has three construction methods, respectively, as follows
Public Drawview (Context context) { Super(context); } Public int defstyleattr) { Super(context, attrs, defstyleattr); } Public Drawview (Context context, AttributeSet attrs) { Super(context, attrs); }
On the construction method, the online explanation is
If a view is instantiated in code, the first constructor is called, and if the definition in XML calls the second constructor, and the third function system is not called, by view (our custom or system-predefined view, So Customtextview and button) are explicitly called, for example here we call the third constructor in the second constructor and pass the R.attr.customizestyle to the third argument. The meaning of the third parameter, as its name implies, is the default style, but it is not clear here that the default style here refers to the default style in the current application or activity theme.
As for a method with only one parameter, we will use it mainly in the new view. The parameter of this is the context environment. Such as
New Button (this);
As for the method invocation of two parameters and three parameters, it is related to whether you have used a style in XML. However, I have verified that, whether there is a style or not, it is simply a constructor that calls two parameters. Verify the following
Custom Style
PackageCom.example.testcode;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;ImportAndroid.view.View; Public classDrawviewextendsView { PublicDrawview (Context context) {Super(context); } PublicDrawview (context context, AttributeSet attrs,intdefstyleattr) { Super(context, attrs, defstyleattr); } PublicDrawview (Context context, AttributeSet attrs) {Super(context, attrs); } @Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) { //TODO auto-generated Method Stub//These two methods must have one, or they will error//super.onmeasure (Widthmeasurespec, heightmeasurespec);Setmeasureddimension (200, 200); } @Overrideprotected voidOnLayout (BooleanChangedintLeftintTopintRight ,intbottom) { //TODO auto-generated Method StubLOG.E ("123", "change===" + changed + "left===" + Left + "top===" + Top + "right===" + Right + "bottom== =" +bottom); Super. OnLayout (changed, left, top, right, bottom); } @Overrideprotected voidOnDraw (canvas canvas) {//TODO auto-generated Method StubLOG.E ("123", "OnDraw"); Super. OnDraw (canvas); }}
The use in XML
1. When there is no style
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context= "Com.example.testcode.MainActivity" > <Com.example.testcode.DrawViewAndroid:layout_width= "100DP"Android:layout_height= "100DP" /></Relativelayout>
Print results
-All:13.445:e/123(6555): drawview_2
2. When there is a style
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context= "Com.example.testcode.MainActivity" > <Com.example.testcode.DrawViewstyle= "@style/test" /></Relativelayout>
Style
<stylename= "Test"> <Itemname= "Android:layout_width">100dp</Item> <Itemname= "Android:layout_height">100dp</Item> <Itemname= "Android:background">#ff0000</Item> </style>
Print results
-All:13.445:e/123(6555): drawview_2
Therefore, the controls in XML only call methods of two parameters. The three-parameter method is actually called explicitly in the two-parameter method. We can look at the button source.
Public classButtonextendsTextView { PublicButton (Context context) { This(Context,NULL); } PublicButton (Context context, AttributeSet attrs) { This(context, attrs, Com.android.internal.r.attr.buttonstyle); } PublicButton (context context, AttributeSet attrs,intdefstyleattr) { This(Context, Attrs, defstyleattr, 0); } PublicButton (context context, AttributeSet attrs,intDefstyleattr,intdefstyleres) { Super(context, Attrs, defstyleattr, defstyleres); } @Override Public voidoninitializeaccessibilityevent (Accessibilityevent event) {Super. Oninitializeaccessibilityevent (event); Event.setclassname (Button.class. GetName ()); } @Override Public voidoninitializeaccessibilitynodeinfo (accessibilitynodeinfo info) {Super. Oninitializeaccessibilitynodeinfo (info); Info.setclassname (Button.class. GetName ()); } }
From the above we have been cleared to see the call relationships of these several construction methods
1.1 parameter->2 parameter (no incoming property)->3 parameter (passed to system default style)->4 parameter
Now let's take a look at the meaning of the last two parameters of the four-parameter construction method
Control call construction method in XML