码迷,mamicode.com
首页 > 其他好文 > 详细

LinearLayout详解二:从其父类View说起

时间:2014-05-18 08:32:48      阅读:751      评论:0      收藏:0      [点我收藏+]

标签:android   源代码   api   

这个View类说来就话长了,但我们又不得不说,要说呢,就得说的彻底,要让大家看得一清二楚,明明白白。所以我们就从源代码角度来看一个view是如何被加载的吧。

如果大家不知道怎么下载android的源代码,或者说懒得去下载(因为源代码确实比较大,大概有10G)的话,教大家几个取巧的办法:

1.直接在google中输入“android view.java”即可。这种方法成功率非常高,一般android的比较重要的类都能搜到。

2.给大家提供一个人家用于放源码的的git:git@gitorious.org:rowboat/frameworks-base.git 大家自己去clone一下。(什么!你Y的居然连git都不会用!服了,好吧,请继续往下看)。

3登录到这里。这是2里面提供的那个git的网页地址,在这里搜索相应要查找的类就可以查找android的源代码了。


从头说起吧,一般android启动的是AndroidManifest.xml中带有

<intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

这样的activity。然后去调用该activity中的

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.frame_content);

	
	}

方法。其中setContentView是把相应的Layout对应的xml解析成view并显示在屏幕上的方法,也就是以上代码中的R.layout.frame_content。

由于activity.java中的代码量太长,我这边就贴个链接,大家看一下:Activity.java

可以发现在1650行的地方有这么一句话:

/**
     * Set the activity content from a layout resource.  The resource will be
     * inflated, adding all top-level views to the activity.
     * 
     * @param layoutResID Resource ID to be inflated.
     */
    public void setContentView(int layoutResID) {
        getWindow().setContentView(layoutResID);
    }
然后我们跳转到738行

/**
     * Retrieve the current {@link android.view.Window} for the activity.
     * This can be used to directly access parts of the Window API that
     * are not available through Activity/Screen.
     * 
     * @return Window The current window, or null if the activity is not
     *         visual.
     */
    public Window getWindow() {
        return mWindow;
    }

发现这个setContentView其实调用的是mWindow的setContentView,mWindow很容易理解,就是 member window嘛,下面我们继续往window里找。
有关window.java的源代码大家可以在这里找到。

很伤心的是,window类是个抽象类

/**
 * Abstract base class for a top-level window look and behavior policy.  An
 * instance of this class should be used as the top-level view added to the
 * window manager. It provides standard UI policies such as a background, title
 * area, default key processing, etc.
 *
 * <p>The only existing implementation of this abstract class is
 * android.policy.PhoneWindow, which you should instantiate when needing a
 * Window.  Eventually that class will be refactored and a factory method
 * added for creating Window instances without knowing about a particular
 * implementation.
 */
public abstract class Window {


,他只是定义了这些方法

/**
     * Convenience for
     * {@link #setContentView(View, android.view.ViewGroup.LayoutParams)}
     * to set the screen content from a layout resource.  The resource will be
     * inflated, adding all top-level views to the screen.
     *
     * @param layoutResID Resource ID to be inflated.
     * @see #setContentView(View, android.view.ViewGroup.LayoutParams)
     */
    public abstract void setContentView(int layoutResID);

    /**
     * Convenience for
     * {@link #setContentView(View, android.view.ViewGroup.LayoutParams)}
     * set the screen content to an explicit view.  This view is placed
     * directly into the screen‘s view hierarchy.  It can itself be a complex
     * view hierarhcy.
     *
     * @param view The desired content to display.
     * @see #setContentView(View, android.view.ViewGroup.LayoutParams)
     */
    public abstract void setContentView(View view);

    /**
     * Set the screen content to an explicit view.  This view is placed
     * directly into the screen‘s view hierarchy.  It can itself be a complex
     * view hierarchy.
     *
     * <p>Note that calling this function "locks in" various characteristics
     * of the window that can not, from this point forward, be changed: the
     * features that have been requested with {@link #requestFeature(int)},
     * and certain window flags as described in {@link #setFlags(int, int)}.
     * 
     * @param view The desired content to display.
     * @param params Layout parameters for the view.
     */
    public abstract void setContentView(View view, ViewGroup.LayoutParams params);

而没有实现它,那真正实现的地方在哪里呢,请猛戳这里PhoneWindow.java 。可爱的你肯定在疑惑,我砸知道的呢,难道我有“第三只眼”!bubuko.com,布布扣靠,其实这个说出来你就会沮丧了,四个字:官方文档。详见:

bubuko.com,布布扣

好,下面,我们开始看PhoneWindow.java 的源码,直接看函数setContentView:

 @Override
    public void setContentView(int layoutResID) {
        if (mContentParent == null) {
            installDecor();
        } else {
            mContentParent.removeAllViews();
        }
        mLayoutInflater.inflate(layoutResID, mContentParent);
        final Callback cb = getCallback();
        if (cb != null) {
            cb.onContentChanged();
        }
    }

可以发现,其实它又调用了mLayoutInflater的inflate方法。我,草!!!妈的,搞这么一层又一层干甚。不写了,且听下回分解。




LinearLayout详解二:从其父类View说起,布布扣,bubuko.com

LinearLayout详解二:从其父类View说起

标签:android   源代码   api   

原文地址:http://blog.csdn.net/zjh171/article/details/26047915

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!