1.LinearLayout context構(gòu)造方法區(qū)別。
官方文檔:
publicView(Contextcontext)
Added inAPI level 1
Simple constructor to use when creating a view from code.
Parameters
contextThe Context the view is running in, through which it can access the current theme, resources, etc.
這種情況為:直接使用系統(tǒng)資源主題來創(chuàng)建view
publicView(Contextcontext,AttributeSetattrs)
Added inAPI level 1
Constructor that is called when inflating a view from XML. This is called when a view is being constructed from an XML file, supplying attributes that were specified in the XML file. This version uses a default style of 0, so the only attribute values applied are those in the Context's Theme and the given AttributeSet.
The method onFinishInflate() will be called after all children have been added.
Parameters
contextThe Context the view is running in, through which it can access the current theme, resources, etc.
attrsThe attributes of the XML tag that is inflating the view.
See Also
View(Context, AttributeSet, int)
以上當(dāng)使用自定義xml時要實(shí)現(xiàn)的構(gòu)造方法。參數(shù)屬性從xml中讀取。
publicView(Contextcontext,AttributeSetattrs, int defStyleAttr)
Added inAPI level 1
Perform inflation from XML and apply a class-specific base style from a theme attribute. This constructor of View allows subclasses to use their own base style when they are inflating. For example, a Button class's constructor would call this version of the super class constructor and supplyR.attr.buttonStylefordefStyleAttr; this allows the theme's button style to modify all of the base view attributes (in particular its background) as well as the Button class's attributes.
Parameters
contextThe Context the view is running in, through which it can access the current theme, resources, etc.
attrsThe attributes of the XML tag that is inflating the view.
defStyleAttrAn attribute in the current theme that contains a reference to a style resource that supplies default values for the view. Can be 0 to not look for defaults.
See Also
暫時未解決。
2.publicView(Contextcontext,AttributeSetattrs) 和onFinishInflate()執(zhí)行先后順序
問題:在做項(xiàng)目的時候,在onFinishInflate()使用findviewbyId,在view第二個構(gòu)造函數(shù),程序崩潰。
查看view第二個構(gòu)造函數(shù)代碼,有一行注釋:The methodonFinishInflate()will be called after all children have been added.
當(dāng)所有控件添加結(jié)束,會執(zhí)行onFinishInflate()方法。