假如,你要自定义一个View,肯定是要继承View,那问题来了,为什么View中有多个构造函数,每个构造函数中参数的含义是什么?
public class SelfView extends View {
public SelfView(Context context) {
super(context);
}
public SelfView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public SelfView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public SelfView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
}
默认的布局文件调用的是前两个构造方法。
View(Context)的介绍如下:当在代码中创建View的时候,最简单的构造函数使用方式。
* 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.
关于View(Context,AttributeSet)的介绍如下:当从XML文件中加载一个View时,这个构造方法将会被调用。当一个View从XML文件创建的时候,XML文件中会提供指定的属性。它在构造函数中事件调用的是View(Context,AttributeSet,int)这个方法,第三个参数传入的值为0,它代表的是默认的风格,所以这些属性的值包括Context的主题以及属性的集合。
当所有子View被添加的时候,将调用onFinishInflate方法。
* 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)
*/
View(Context, AttributeSet, int)的介绍如下:从XML文件中执行加载操作,从theme属性中应用一个特定类的基本样式。View的构造方法允许子类在加载View的时候使用它们自己的基本样式。
* 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)
View(Context, AttributeSet, int,int) 介绍如下所示。当从XML文件中执行加载操作时,从一个主题属性或者style资源文件中应用特定类的基本样式。View的构造方法允许在加载的时候子类使用它们自己的基本样式。在确定最终属性的最终值的时候,有四种输入会起到作用。1.指定在AttributeSet中名为style的资源文。2.指定在defStyleAttr的默认style。3。指定在defStyleRes的默认style。4.在theme中的基本值。那这四种输入,最终选用谁的值呢。1,2,3,4顺序考虑,优先级依次降低。
* 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)