Android中每个控件都会在界面上占据一块矩形的区域,在Android中控件被分为两种,①VIew ②ViewGroup ViewGroup可以包含多个View 或者Viewgroup
这样在界面控件上就会形成一棵控件树
上层控件负责下层控件的测量和绘制,并传递交互事件,我们经常使用的findViewById()就是在控件树中以树的深度优先遍历来查找.。每一棵控件树的顶部都有一个顶级的ViewGroup(DecorView),是控制树的核心,所有的交互事件都有它统一调度和分配。
下面介绍android的界面框架:
每一个的Activity都会包含一个Window对象(Window为抽象类),一般由其继承类PhoneWindow来实现,而DecorVewi作为整个应用窗口的根ViewGroup
Decor 是PhoneWindow内部类,FrameLayout的 子类
而最顶层的ViewGroup 可以根据不同的Theme 来设置,比如默认情况下有ActionBar 采用的ViewGroup就是LinearLayout ,
android:orientation="vertical"
android:fitsSystemWindows="true">
android:layout_width="match_parent"
android:layout_height="?android:attr/windowTitleSize"
style="?android:attr/windowTitleBackgroundStyle">
style="?android:attr/windowTitleStyle"
android:background="@null"
android:fadingEdge="horizontal"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:foregroundGravity="fill_horizontal|top"
android:foreground="?android:attr/windowContentOverlay" />
而当fullscreen的时候 ViewGroup 就是
android:id="@android:id/content"
android:fitsSystemWindows="true"
android:foregroundInsidePadding="false"
android:foregroundGravity="fill_horizontal|top"
android:foreground="?android:attr/windowContentOverlay" />
而上面红色加粗的ViewGroup id 为 conten , 它将会赋值给PhoneWindow中的一个成员变量,
这个id为content的ViewGroup ,即mContentParent ,它就是放置activity的布局的如activity_main.xml
至于
requestWindowFeature(Window.FEATURE_NO_TITLE);
这个方法,
public classMainActivity extendsActivity { 时,能成功设置
在
public classMainActivity extendsAppCompatActivity 通过
this.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
来设置
因为在AppCompatActivity中
/*** Enable extended window features. This should be called instead of* {@linkandroid.app.Activity#requestWindowFeature(int)} or* {@linkandroid.view.Window#requestFeature getWindow().requestFeature()}.*
还有就是想到一个问题,通过代码
requestWindowFeature(Window.FEATURE_NO_TITLE);
和通过主题NoTitle 到底有什么不一样,最后查资料和看源代码,发现其实最后调用的都是Window的requestFeature方法,
至于为什么要在setContentView之前requestFeature ,在源码有解释,
@Overridepublic booleanrequestFeature(intfeatureId) {
if(mContentParent!= null) {
throw newAndroidRuntimeException("requestFeature() must be called before adding content");} 前面说到 mContentParent 是在setContentView中 被赋值的(id为content的ViewGroup),当在setContentView之后再设置requestFeature的话mContentParent 就不是为空了,那时候就会抛出异常了
相关博客资料参考:
android群英传
第一天,加油