Android UI | 從setContentView 了解View的繪制過程

setContentView 方法如下:

public void setContentView(@LayoutRes int layoutResID) {
        getWindow().setContentView(layoutResID);
        initWindowDecorActionBar();
    }

getWindow() , 實際上就是PhoneWindow , 繼續(xù)查看PhoneWindow類中的setContentView方法

public void setContentView(int layoutResID) {
    // Note: FEATURE_CONTENT_TRANSITIONS may be set in the process of installing the window
    // decor, when theme attributes and the like are crystalized. Do not check the feature
    // before this happens.
    if (mContentParent == null) {
       ///  這里初始化 decor 
        installDecor();
    } else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
        mContentParent.removeAllViews();
    }

    if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
        final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,
                getContext());
        transitionTo(newScene);
    } else {
        //  這里是不是很眼熟箱歧,可以猜測是加載布局文件巧婶,
        mLayoutInflater.inflate(layoutResID, mContentParent);
    }
    mContentParent.requestApplyInsets();
    final Callback cb = getCallback();
    if (cb != null && !isDestroyed()) {
        cb.onContentChanged();
    }
    mContentParentExplicitlySet = true;
}

繼續(xù)查看如何初始化 decor

private void installDecor() {
  if (mDecor == null) {
       // 初始化decor
        mDecor = generateDecor(-1);
        mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

        mDecor.setIsRootNamespace(true);
        if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {
            mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
        }
    } else {
        // 設置DecorView的window 為PhoneWindow對象
        mDecor.setWindow(this);
    }
  if (mContentParent == null) {
      // 生成內(nèi)容根節(jié)點
      mContentParent = generateLayout(mDecor);
  }
  // ...其他代碼忽略
}

先查看 generateDecor

protected DecorView generateDecor(int featureId) {
    // System process doesn't have application context and in that case we need to directly use
    // the context we have. Otherwise we want the application context, so we don't cling to the
    // activity.
    Context context;
    if (mUseDecorContext) {
        Context applicationContext = getContext().getApplicationContext();
        if (applicationContext == null) {
            context = getContext();
        } else {
            context = new DecorContext(applicationContext, getContext());
            if (mTheme != -1) {
                context.setTheme(mTheme);
            }
        }
    } else {
        context = getContext();
    }
    return new DecorView(context, featureId, this, getAttributes());
}

創(chuàng)建DecorView 對象

在看看 generateLayout(decorView)

protected ViewGroup generateLayout(DecorView decor) {
    // .....
    if (a.getBoolean(R.styleable.Window_windowNoTitle, false)) {
        requestFeature(FEATURE_NO_TITLE);
    } else if (a.getBoolean(R.styleable.Window_windowActionBar, false)) {
        // Don't allow an action bar if there is no title.
        // 這里的代碼比較眼熟封断,設置主體的時候會用到
        requestFeature(FEATURE_ACTION_BAR);
    }
  
    /// ...
  
    // Inflate the window decor.
    int layoutResource;
    // 這里獲取一個features , 根據(jù)這個值獲取一個布局文件
    int features = getLocalFeatures();
    //  那最后一個布局查看
    if(...){...}
    else if ((features & (1 << FEATURE_ACTION_MODE_OVERLAY)) != 0) {
        layoutResource = R.layout.screen_simple_overlay_action_mode;
    } else {
        // Embedded, so no decoration is needed.
        layoutResource = R.layout.screen_simple;
        // System.out.println("Simple!");
    }
    // 這個方法也很重要犁钟, 通過DecorView 加載上面獲取到的布局文件
    mDecor.onResourcesLoaded(mLayoutInflater, layoutResource);
    // 這里是拿到哪個id 為@android:id/content 的對象
    ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);
    // ... 
    // 最后返回這個content饵筑,那由此我們可以知道 PhoneWindow的mContentParent 對象其實就是id為@android:id/content 的FrameLayout對象
    return contentParent;
}

screen_simple布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">
    <ViewStub android:id="@+id/action_mode_bar_stub"
          android:inflatedId="@+id/action_mode_bar"
          android:layout="@layout/action_mode_bar"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:theme="?attr/actionBarTheme" />
    <FrameLayout
         android:id="@android:id/content"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:foregroundInsidePadding="false"
         android:foregroundGravity="fill_horizontal|top"
         android:foreground="?android:attr/windowContentOverlay" />
</LinearLayout>

這里就是一個普通的LinearLayout , 重點是里面有個FrameLayout而且id為@android:id/content 阻塑, 這個就是我們自定義開發(fā)布局都會放在這個FrameLayout 里面

現(xiàn)在再回過頭來看看setContentView 方法里面的 mLayoutInflater.inflate(layoutResID, mContentParent);

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
    return inflate(resource, root, root != null);
}

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
    final Resources res = getContext().getResources();
    // 這個根據(jù)名字可以猜出乳乌,是預加載布局文件
    View view = tryInflatePrecompiled(resource, res, root, attachToRoot);
    if (view != null) {
        return view;
    }
    // 這個是獲取xml資源解析器
    XmlResourceParser parser = res.getLayout(resource);
    try {
        // 重要的是這個方法
        return inflate(parser, root, attachToRoot);
    } finally {
        parser.close();
    }
}

繼續(xù)查看 inflate(parser, root, attachToRoot);

public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
    synchronized (mConstructorArgs) {
        final Context inflaterContext = mContext;
        // 拿到所有的屬性
        final AttributeSet attrs = Xml.asAttributeSet(parser);
        Context lastContext = (Context) mConstructorArgs[0];
        mConstructorArgs[0] = inflaterContext;
        // 這個result 就算 decorView
        View result = root;
         if (TAG_MERGE.equals(name)) {
               //  這里判斷了是否是 <merge>解點袱蜡, 不能是最外層的根節(jié)點
              if (root == null || !attachToRoot) {
                    throw new InflateException("<merge /> can be used only with a valid "
                            + "ViewGroup root and attachToRoot=true");
                }
              // 繼續(xù)通過遞歸調(diào)用的方式,加載出所有的子節(jié)點互亮,并放在root節(jié)點犁享,即decorView 下面
              rInflate(parser, root, inflaterContext, attrs, false);
          }
    }
}

/// finishInflate 是否停止解析
void rInflate(XmlPullParser parser, View parent, Context context,
        AttributeSet attrs, boolean finishInflate) throws XmlPullParserException, IOException {

    final int depth = parser.getDepth();
    int type;
    boolean pendingRequestFocus = false;

    while (((type = parser.next()) != XmlPullParser.END_TAG ||
            parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {

        if (type != XmlPullParser.START_TAG) {
            continue;
        }

        final String name = parser.getName();

        if (TAG_REQUEST_FOCUS.equals(name)) {
            pendingRequestFocus = true;
            consumeChildElements(parser);
        } else if (TAG_TAG.equals(name)) {
            parseViewTag(parser, parent, attrs);
        } else if (TAG_INCLUDE.equals(name)) {
            // 解析include 標簽,不能是根節(jié)點標簽
            if (parser.getDepth() == 0) {
                throw new InflateException("<include /> cannot be the root element");
            }
            parseInclude(parser, context, parent, attrs);
        } else if (TAG_MERGE.equals(name)) {
            throw new InflateException("<merge /> must be the root element");
        } else {
            final View view = createViewFromTag(parent, name, context, attrs);
            final ViewGroup viewGroup = (ViewGroup) parent;
            final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);
            // 再次遞歸調(diào)用豹休,解析子節(jié)點
            rInflateChildren(parser, view, attrs, true);
            // 把解析出來的子節(jié)點炊昆,放在上級節(jié)點view[]里面
            viewGroup.addView(view, params);
        }
    }

    if (pendingRequestFocus) {
        parent.restoreDefaultFocus();
    }
    // 停止解析
    if (finishInflate) {
        parent.onFinishInflate();
    }
}

通過上面一層一層的解析,就能把我們自己定義的布局威根,解析出來凤巨, 并放在mContentParent里面, 這個就是@android:id/content的FrameLayout洛搀。

到這一步敢茁,好像還缺點啥,還沒看到 mContentParent , 是如何放進DecorView 里面的留美。再回頭看看PhoneWindow類里面的 generateLayout方法彰檬,剛才說的有一個加載資源的方法,我們漏看了
mDecor.onResourcesLoaded(mLayoutInflater, layoutResource);
這個方法谎砾,從表面上看逢倍, 是拿到了某個主題的資源文件,并解析

void onResourcesLoaded(LayoutInflater inflater, int layoutResource) {
    // ... 
    mDecorCaptionView = createDecorCaptionView(inflater);
    // 這一步就是解析系統(tǒng)主題中的某個帶有@android:id/content的哪個布局
    final View root = inflater.inflate(layoutResource, null);
    if (mDecorCaptionView != null) {
        if (mDecorCaptionView.getParent() == null) {
            addView(mDecorCaptionView,
                    new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
        }
        mDecorCaptionView.addView(root,
                new ViewGroup.MarginLayoutParams(MATCH_PARENT, MATCH_PARENT));
    } else {

        // Put it below the color views.
        // 在這一步景图,把mContentParent 添加到 decorView 的第一個元素里面了
        addView(root, 0, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
    }
    mContentRoot = (ViewGroup) root;
    initializeElevation();
}

到這一步较雕,基本上都清晰了

image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市挚币,隨后出現(xiàn)的幾起案子亮蒋,更是在濱河造成了極大的恐慌,老刑警劉巖忘晤,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件宛蚓,死亡現(xiàn)場離奇詭異,居然都是意外死亡设塔,警方通過查閱死者的電腦和手機凄吏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來闰蛔,“玉大人痕钢,你說我怎么就攤上這事⌒蛄” “怎么了任连?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長例诀。 經(jīng)常有香客問我随抠,道長裁着,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任拱她,我火速辦了婚禮二驰,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘秉沼。我一直安慰自己桶雀,他們只是感情好,可當我...
    茶點故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布唬复。 她就那樣靜靜地躺著矗积,像睡著了一般。 火紅的嫁衣襯著肌膚如雪敞咧。 梳的紋絲不亂的頭發(fā)上棘捣,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天,我揣著相機與錄音妄均,去河邊找鬼柱锹。 笑死,一個胖子當著我的面吹牛丰包,可吹牛的內(nèi)容都是我干的禁熏。 我是一名探鬼主播,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼邑彪,長吁一口氣:“原來是場噩夢啊……” “哼瞧毙!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起寄症,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤宙彪,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后有巧,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體释漆,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年篮迎,在試婚紗的時候發(fā)現(xiàn)自己被綠了男图。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡甜橱,死狀恐怖逊笆,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情岂傲,我是刑警寧澤难裆,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響乃戈,放射性物質(zhì)發(fā)生泄漏褂痰。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一偏化、第九天 我趴在偏房一處隱蔽的房頂上張望脐恩。 院中可真熱鬧,春花似錦侦讨、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至崇猫,卻和暖如春沈条,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背诅炉。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工蜡歹, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人涕烧。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓月而,卻偏偏與公主長得像,于是被迫代替她去往敵國和親议纯。 傳聞我的和親對象是個殘疾皇子父款,可洞房花燭夜當晚...
    茶點故事閱讀 44,724評論 2 354