这个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> public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(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;
}很伤心的是,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 的源码,直接看函数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();
}
}
LinearLayout详解二:从其父类View说起,布布扣,bubuko.com
原文:http://blog.csdn.net/zjh171/article/details/26047915