学习了一下Android View构造函数和theme.obtain

来源:互联网 发布:最大的骗局 知乎 编辑:程序博客网 时间:2024/06/03 16:06

1. public View(Context context);

/**

     * 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.

     */

   当从代码创建的时候使用的简单的构造器

Param:

(1)context:view运行在其中的上下文,通过他可以接触到当前的主题,资源,等等.


2.public View(Context context, AttributeSet attrs);

 /**

     * 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)

     */

当视图被XML文件填充时调用的构造器.当一个视图从XML文件构造时被调用,提供在XML文件中被指定的属性.这个版本使用的默认风格是0,所以只有Context中的主题和AttributeSet提供属性值.

在所有的子视图被添加到父视图后,方法onFinishInflate()将被调用.

Param:

(1)context:view运行在其中的上下文,通过他可以接触到当前的主题,资源,等等.

(2)attrs:正在填充视图的XML标识的属性


3.public View(Context context, AttributeSet attrs, int defStyleAttr) ;

  /**

     * 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)


     */


 Perform inflation from XML and apply a class-specific base style from a theme attribute

从XML并且提供一个指定的类基础的风格从一个主题的属性执行填充

从XML和一个指定类的基础风格(在主题中的属性里指定的)进行填充视图.这个构造器允许子类使用他们自己的基础风格当他们被填充的时候.比如:一个Button类的构造器将会调用父类构造器的这个版本,并且提供R.attr.buttonStyle当做defStyleAttr. 这允许主题的 button 风格来修改所有的基础view属性(使他的背景为特殊的)和Button类的属性一样.

Param:

(1)context:view运行在其中的上下文,通过他可以接触到当前的主题,资源,等等.

(2)attrs:正在填充视图的XML标识的属性

(3)defStyleAttr:在当前的主题中的一个属性,包含一个风格资源的一个引用,用来提供视图的默认值.0代表不查看默认值



3.public View(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) ;

/**

     * 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>&lt;Button * textColor="#ff000000"&gt;</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)


     */


从XML,当前主题中的指定类的基本风格引用属性,或者是一个风格资源填充视图.这个构造器允许子类在填充的时候使用他们自己的基础风格.

当决定最后的属性值时,这里有四个输入可以参与:

1.在给定的AttributeSet中给定的任何属性值

2.在AttributeSet中指定的风格资源(被叫做”style”)

3.被defStyleAttr指定的默认的风格

4.被defStyleRes指定的默认风格

5.在主题中的基础值


这些输入的使用与否是根据他们的顺序来决定的.换句话说,如果AttrbuteSet中,你提供了一个Button textColor=“#ff000000”,那么按钮的文本将会一直是黑色,不管在风格中指定为什么值


Param:

(1)context:view运行在其中的上下文,通过他可以接触到当前的主题,资源,等等.

(2)attrs:正在填充视图的XML标识的属性

(3)defStyleAttr:在当前的主题中的一个属性,包含一个风格资源的一个引用,用来提供视图的默认值.0代表不查看默认值


(4)defStyleRes:一个风格资源的资源标识,为view提供默认值,当且仅当defStyleAttr为0时,或者不能在主题中找到的时候使用.为0则不查询默认值


5.public TypedArray obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)

  /**

         * Return a TypedArray holding the attribute values in

         * <var>set</var>

         * that are listed in <var>attrs</var>.  In addition, if the given

         * AttributeSet specifies a style class (through the "style" attribute),

         * that style will be applied on top of the base attributes it defines.

         * 

         * <p>Be sure to call{@link TypedArray#recycle() TypedArray.recycle()} when you are done

         * with the array.

         * 

         * <p>When determining the final value of a particular attribute, there

         * are four inputs that come into play:</p>

         * 

         * <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> and

         *     <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>&lt;Button

         * textColor="#ff000000"&gt;</code>, then the button's text will

         * <em>always</em> be black, regardless of what is specified in any of

         * the styles.

         * 

         * @param set The base set of attribute values.  May be null.

         * @param attrs The desired attributes to be retrieved.

         * @param defStyleAttr An attribute in the current theme that contains a

         *                     reference to a style resource that supplies

         *                     defaults values for the TypedArray.  Can be

         *                     0 to not look for defaults.

         * @param defStyleRes A resource identifier of a style resource that

         *                    supplies default values for the TypedArray,

         *                    used only if defStyleAttr is 0 or can not be found

         *                    in the theme.  Can be 0 to not look for defaults.

         * 

         * @return Returns a TypedArray holding an array of the attribute values.

         * Be sure to call {@link TypedArray#recycle() TypedArray.recycle()}


         * when done with it.



返回一个TpyeArray保持在attrs中所列出来的集合中的属性值.附加的,如果个顶的AttributeSet 指定了一个style类(通过”style"属性设置),那么这个style将被应用于他定义的最顶层的基础属性.

确认调用TypedArray.recycle()方法,当你使用完typearray的时候.

当决定最后的特殊属性值时,这里有四个输入可以充当:

1.在AttributeSet中给出的任何一个属性值

2.在AttributeSet中被指定的style资源文件(名字是”style”).

3.defStyleAttr和defStyleRes指定的默认的style

4.主题的基础值


6. public TypedArray obtainStyledAttributes(int resid, int[] attrs);


 /**

         * Return a TypedArray holding the values defined by the style

         * resource <var>resid</var> which are listed in<var>attrs</var>.

         * 

         * <p>Be sure to call{@link TypedArray#recycle() TypedArray.recycle()} when you are done

         * with the array.

         * 

         * @param resid The desired style resource.

         * @param attrs The desired attributes in the style.

         * 

         * @throws NotFoundException Throws NotFoundException if the given ID does not exist.

         * 

         * @return Returns a TypedArray holding an array of the attribute values.

         * Be sure to call {@link TypedArray#recycle() TypedArray.recycle()}

         * when done with it.

         * 

         * @see Resources#obtainAttributes

         * @see #obtainStyledAttributes(int[])

         * @see #obtainStyledAttributes(AttributeSet, int[], int, int)


         */


返回一个TpyeArray保持在attrs中所列出来的集合中的属性值.附加的,如果个顶的AttributeSet 指定了一个style类(通过”style"属性设置),那么这个style将被应用于他定义的最顶层的基础属性.

确认调用TypedArray.recycle()方法,当你使用完typearray的时候.


7. public TypedArray obtainStyledAttributes(int[] attrs);

 /**

         * Return a TypedArray holding the values defined by

         * <var>Theme</var> which are listed in<var>attrs</var>.

         * 

         * <p>Be sure to call{@link TypedArray#recycle() TypedArray.recycle()} when you are done

         * with the array.

         * 

         * @param attrs The desired attributes.

         *

         * @throws NotFoundException Throws NotFoundException if the given ID does not exist.

         * 

         * @return Returns a TypedArray holding an array of the attribute values.

         * Be sure to call {@link TypedArray#recycle() TypedArray.recycle()}

         * when done with it.

         * 

         * @see Resources#obtainAttributes

         * @see #obtainStyledAttributes(int, int[])

         * @see #obtainStyledAttributes(AttributeSet, int[], int, int)


         */

0 0