首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android2.3 View视图框架源码分析之一:android是怎么创建一个view的

2012-07-25 
android2.3 View视图框架源码分析之一:android是如何创建一个view的?View是所有控件的一个基类,无论是布局

android2.3 View视图框架源码分析之一:android是如何创建一个view的?
View是所有控件的一个基类,无论是布局(Layout),还是控件(Widget)都是继承自View类。只不过layout是一个特殊的view,它里面创建一个view的数组可以包含其他的view而已。
这一篇文章把所有的layout和widget都统称为view,那么android是如何创建一个view的呢?


一。在代码中直接new出来。
比如说你要创建一个TextView的实例,那么你可以这样写:



在类android.content.res.Resources类中获取XmlResourceParser对象;


android.content.res.AssetManager类



三 。通过view.findViewById(resourceid)获得一个view的实例
android.View.View类中



找到com.android.internal.policy.impl.Policy类

private ViewGroup mContentParent;//这是window的顶层视图,它包含一些窗口的装饰,比图title bar,状态栏等等    private DecorView mDecor;    //这里的layoutResID也是由mLayoutInflater进行加载的,加载的方式与第二种方法一样。//只不过这里把的到的view变成了mContentParent的子view   @Override    public void setContentView(int layoutResID) {        if (mContentParent == null) {            installDecor();        } else {            mContentParent.removeAllViews();        }        mLayoutInflater.inflate(layoutResID, mContentParent);        final Callback cb = getCallback();        if (cb != null) {//这是回调方法,表明mContentParent的子view已经发生改变            cb.onContentChanged();        }    }    //再来看看mContentParent究竟是何物,它肯定是一个viewGroupprivate void installDecor() {        if (mDecor == null) {            mDecor = generateDecor();            mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);            mDecor.setIsRootNamespace(true);        }        if (mContentParent == null) {            mContentParent = generateLayout(mDecor);            mTitleView = (TextView)findViewById(com.android.internal.R.id.title);            if (mTitleView != null) {//这里设置的是是否隐藏titleContainer,即头部titlebar                if ((getLocalFeatures() & (1 << FEATURE_NO_TITLE)) != 0) {                    View titleContainer = findViewById(com.android.internal.R.id.title_container);                    if (titleContainer != null) {                        titleContainer.setVisibility(View.GONE);                    } else {                        mTitleView.setVisibility(View.GONE);                    }                    if (mContentParent instanceof FrameLayout) {                        ((FrameLayout)mContentParent).setForeground(null);                    }                } else {                    mTitleView.setText(mTitle);                }            }        }    }  //当顶层view为null是,new了一个DecorView protected DecorView generateDecor() {        return new DecorView(getContext(), -1);    }    //这里生成了mContentParent。protected ViewGroup generateLayout(DecorView decor) {          mDecor.startChanging();        //根据window的不同参数选择layoutResource        View in = mLayoutInflater.inflate(layoutResource, null);        decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));        ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);        if (contentParent == null) {            throw new RuntimeException("Window couldn't find content container view");        }        return contentParent;    }    //顶层view是一个framelayoutprivate final class DecorView extends FrameLayout implements RootViewSurfaceTaker { public DecorView(Context context, int featureId) {super(context);mFeatureId = featureId;        }}//下面说明findVIewById//首先是获取顶层view,即继承自FrameLayout的viewgorup@Override    public final View getDecorView() {        if (mDecor == null) {            installDecor();        }        return mDecor;    }//然后mDecor.findViewById根据id获取它的子view//这里就是通过第三种方法获取它的子view

热点排行