android 群英传笔记,Android 群英传读书笔记1

Android中每个控件都会在界面上占据一块矩形的区域,在Android中控件被分为两种,①VIew ②ViewGroup  ViewGroup可以包含多个View 或者Viewgroup

这样在界面控件上就会形成一棵控件树

0818b9ca8b590ca3270a3433284dd417.png

上层控件负责下层控件的测量和绘制,并传递交互事件,我们经常使用的findViewById()就是在控件树中以树的深度优先遍历来查找.。每一棵控件树的顶部都有一个顶级的ViewGroup(DecorView),是控制树的核心,所有的交互事件都有它统一调度和分配。

下面介绍android的界面框架:

0818b9ca8b590ca3270a3433284dd417.png

每一个的Activity都会包含一个Window对象(Window为抽象类),一般由其继承类PhoneWindow来实现,而DecorVewi作为整个应用窗口的根ViewGroup

0818b9ca8b590ca3270a3433284dd417.png

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中的一个成员变量,

0818b9ca8b590ca3270a3433284dd417.png

这个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群英传

第一天,加油

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值