View的五個(gè)構(gòu)造函數(shù)的區(qū)別

image.png
  1. 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);
    }

  1. 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) {...}

  1. 從布局文件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);
    }

  1. 三參構(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的子類傳自己用的屬性配置的沙合。


  1. 四參構(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>&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)
     */
    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)行初始化操作硝逢。

總結(jié):View(Context)是從代碼中創(chuàng)建view走的構(gòu)造鱼炒。 View(Context,AttributeSet),是從xml中填充的view走的構(gòu)造,2參會(huì)調(diào)用3參欲芹,3參調(diào)用4參,4參最后會(huì)調(diào)用1參菱父,然后拿到所有屬性再初始化。這樣子類的構(gòu)造可以傳入特定的attr和style來(lái)初始化浙宜。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市粟瞬,隨后出現(xiàn)的幾起案子萤捆,更是在濱河造成了極大的恐慌,老刑警劉巖鳖轰,帶你破解...
    沈念sama閱讀 216,591評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異蕴侣,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)昆雀,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)蝠筑,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人什乙,你說(shuō)我怎么就攤上這事已球。” “怎么了智亮?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,823評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)阔蛉。 經(jīng)常有香客問(wèn)我,道長(zhǎng)状原,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,204評(píng)論 1 292
  • 正文 為了忘掉前任颠区,我火速辦了婚禮,結(jié)果婚禮上瓦呼,老公的妹妹穿的比我還像新娘。我一直安慰自己央串,他們只是感情好磨澡,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,228評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布稳摄。 她就那樣靜靜地躺著,像睡著了一般饲宿。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上瘫想,一...
    開(kāi)封第一講書(shū)人閱讀 51,190評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音减噪,去河邊找鬼。 笑死筹裕,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的朝卒。 我是一名探鬼主播,決...
    沈念sama閱讀 40,078評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼抗斤,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了洞拨?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 38,923評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤负拟,失蹤者是張志新(化名)和其女友劉穎掩浙,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體秸歧,經(jīng)...
    沈念sama閱讀 45,334評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,550評(píng)論 2 333
  • 正文 我和宋清朗相戀三年键菱,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片经备。...
    茶點(diǎn)故事閱讀 39,727評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖造虎,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情算凿,我是刑警寧澤,帶...
    沈念sama閱讀 35,428評(píng)論 5 343
  • 正文 年R本政府宣布氓轰,位于F島的核電站浸卦,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜储玫,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,022評(píng)論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望匣椰。 院中可真熱鬧,春花似錦端礼、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,672評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)凡桥。三九已至蟀伸,卻和暖如春缅刽,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背衰猛。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,826評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留啡省,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,734評(píng)論 2 368
  • 正文 我出身青樓卦睹,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親分预。 傳聞我的和親對(duì)象是個(gè)殘疾皇子兢交,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,619評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容