1.LinearLayout context构造方法区别。
官方文档:
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.
这种情况为:直接使用系统资源主题来创建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)
以上当使用自定义xml时要实现的构造方法。参数属性从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()执行先后顺序
问题:在做项目的时候,在onFinishInflate()使用findviewbyId,在view第二个构造函数,程序崩溃。
查看view第二个构造函数代码,有一行注释:The methodonFinishInflate()will be called after all children have been added.
当所有控件添加结束,会执行onFinishInflate()方法。
网友评论