博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在进程View时的四个构造函数详解
阅读量:5088 次
发布时间:2019-06-13

本文共 4115 字,大约阅读时间需要 13 分钟。

public View(Context context): 源代码中的解释如下:在Code中实例化一个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.     */
 
public View(Context context, @Nullable AttributeSet attrs): 源代码解释如下:在xml中定义会调用第二个构造函数
/**     * 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. * * @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, int defStyleAttr): 源代码解释如下:允许继承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 R.attr.buttonStyle for defStyleAttr; 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, 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.     * 

* When determining the final value of a particular attribute, there are * four inputs that come into play: *

    *
  1. Any attribute values in the given AttributeSet. *
  2. The style resource specified in the AttributeSet (named "style"). *
  3. The default style specified by defStyleAttr. *
  4. The default style specified by defStyleRes. *
  5. The base values in this theme. *
*

* 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 <Button * textColor="#ff000000"> * , then the button's text will always 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) */

 

 

 

 

 

 

转载于:https://www.cnblogs.com/liaojie970/p/5846435.html

你可能感兴趣的文章
ionic2+ 基础
查看>>
互联网模式下我们更加应该“专注”
查看>>
myeclipse集成jdk、tomcat8、maven、svn
查看>>
查询消除重复行
查看>>
Win 10 文件浏览器无法打开
查看>>
HDU 1212 Big Number(C++ 大数取模)(java 大数类运用)
查看>>
-bash: xx: command not found 在有yum源情况下处理
查看>>
[leetcode]Minimum Path Sum
查看>>
内存管理 浅析 内存管理/内存优化技巧
查看>>
hiho1079 线段树区间改动离散化
查看>>
【BZOJ 5222】[Lydsy2017省队十连测]怪题
查看>>
第二次作业
查看>>
【input】 失去焦点时 显示默认值 focus blur ★★★★★
查看>>
Java跟Javac,package与import
查看>>
day-12 python实现简单线性回归和多元线性回归算法
查看>>
Json格式的字符串转换为正常显示的日期格式
查看>>
[转]使用 Razor 进行递归操作
查看>>
[转]Android xxx is not translated in yyy, zzz 的解决方法
查看>>
docker入门
查看>>
Android系统--输入系统(十一)Reader线程_简单处理
查看>>