-
View()
,沒(méi)有暴露出來(lái),等于沒(méi)有疾捍,過(guò)過(guò)過(guò)
/**
* Non-public constructor for use in testing
*/
View() {
mResources = null;
mRenderNode = RenderNode.create(getClass().getName(), this);
}
-
View(Context context)
,當(dāng)在代碼中創(chuàng)建view時(shí)使用,傳入一個(gè)上下文乱豆,使該View運(yùn)行在其中,并且可以通過(guò)這個(gè)上下文獲取主題宛裕、資源等。
/**
* Simple constructor to use when creating a view from code.
*
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
*/
public View(Context context) {...}
- 從布局文件xml中填充出來(lái)的view使用這個(gè)2參的構(gòu)造函數(shù)揩尸。
第1個(gè)參數(shù)context
上文說(shuō)過(guò)了,第2個(gè)參數(shù)attr
包含了在xml中定義的屬性岩榆,初始化的時(shí)候需要把這些屬性拿出來(lái)用∮卤撸可以看到這個(gè)構(gòu)造直接調(diào)用了一個(gè)三參構(gòu)造。那么繼續(xù)走
/**
* 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.
*
* <p>
* The method onFinishInflate() will be called after all children have been
* added.
*
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
* @param attrs The attributes of the XML tag that is inflating the view.
* @see #View(Context, AttributeSet, int)
*/
public View(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
- 三參構(gòu)造
/**
* 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
* supply <code>R.attr.buttonStyle</code> for <var>defStyleAttr</var>; 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.
*
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
* @param attrs The attributes of the XML tag that is inflating the view.
* @param defStyleAttr An 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 #View(Context, AttributeSet)
*/
public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
2參構(gòu)造會(huì)調(diào)用這個(gè)3參構(gòu)造识颊,多了一個(gè)默認(rèn)樣式defStyleAttr
,比如Button
的構(gòu)造會(huì)傳一個(gè)Button的樣式祥款,然后調(diào)用了自己的四參構(gòu)造
public Button(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.buttonStyle);
}
而四參構(gòu)造會(huì)調(diào)用父類TextView
的四參構(gòu)造清笨,TextView
會(huì)繼續(xù)調(diào)用父類View
的四參構(gòu)造镰踏。
public Button(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
所以這個(gè)三參構(gòu)造的第三個(gè)參數(shù)defStyleAttr
是給View
的子類傳自己用的屬性配置的沙合。
- 四參構(gòu)造
/**
* Perform inflation from XML and apply a class-specific base style from a
* theme attribute or style resource. This constructor of View allows
* subclasses to use their own base style when they are inflating.
* <p>
* When determining the final value of a particular attribute, there are
* four inputs that come into play:
* <ol>
* <li>Any attribute values in the given AttributeSet.
* <li>The style resource specified in the AttributeSet (named "style").
* <li>The default style specified by <var>defStyleAttr</var>.
* <li>The default style specified by <var>defStyleRes</var>.
* <li>The base values in this theme.
* </ol>
* <p>
* Each of these inputs is considered in-order, with the first listed taking
* precedence over the following ones. In other words, if in the
* AttributeSet you have supplied <code><Button * textColor="#ff000000"></code>
* , then the button's text will <em>always</em> be black, regardless of
* what is specified in any of the styles.
*
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
* @param attrs The attributes of the XML tag that is inflating the view.
* @param defStyleAttr An 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.
* @param defStyleRes A resource identifier of a style resource that
* supplies default values for the view, used only if
* defStyleAttr is 0 or can not be found in the theme. Can be 0
* to not look for defaults.
* @see #View(Context, AttributeSet, int)
*/
public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
this(context);
final TypedArray a = context.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.View, defStyleAttr, defStyleRes);
...
}
比三參多了個(gè)defStyleRes
,子類在覆寫(xiě)構(gòu)造的時(shí)候可以傳入樣式資源到這里绊率。可以看到滤否,四參構(gòu)造也是先調(diào)用了this(context)
窒所,然后獲取到TypeArray
拿各種屬性,再繼續(xù)對(duì)view進(jìn)行初始化操作硝逢。