LinearLayout幾個構(gòu)造參數(shù)疑點(diǎn)

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

View(Context, AttributeSet)

暫時未解決。

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()方法。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容