最近项目要求要用Android,所以快速的学习了一些关于Android的知识,下面对view这个组件做一个小小的总结:
View的继承关系如下图:
1)Android的所有UI组件都继承了View类
2)查看ViewGroup的api,可以看到这么一句A ViewGroup
is a special view that can contain other views (called children.),也就是说ViewGroup可以包含其他的View,由于ViewGroup是继承自View,所以ViewGroup也可以包含ViewGroup组件。很明显的这是典型的组合模式的应用:我们知道组合模式可以把对象组织到树结构中,用来描述部分和整体之间的关系。
GroupView和View之间的组合关系可以通过ViewGroup的源码来体现出来
public abstract class ViewGroup extends View implements ViewParent, ViewManager { private static final String TAG = "ViewGroup"; private static final boolean DBG = false; /** * Views which have been hidden or removed which need to be animated on * their way out. * This field should be made private, so it is hidden from the SDK. * {@hide} */ protected ArrayList<View> mDisappearingChildren; // The view contained within this ViewGroup that has or contains focus. private View mFocused;
所以他们之间的树形结构图如下所示:
注意途中的View代表的是父类View的子类实例
(关于组合模式的详细情况,参考《Java与模式》组合模式章节)
原文:http://blog.csdn.net/chunqiuwei/article/details/23286521