Window相关(艺术探索读书笔记)

2019-07-05  本文已影响0人  最美下雨天

Window是一个抽象类,具体实现是PhoneWindow,如果我们要创建一个Window必须要通过Window的管理类(WindowManager)来完成,而Window的具体的实现是在WindowManagerService中

也就是说创建Window的过程就是WindowManager和WindowManagerService通过IPC交互的一个过程

相关类作用以及之间的关联

ViewRootImpl、DecorView、Window、WindowManager、WindowManagerService、WindowManagerGlobal

DecorView:系统根据不同的theme会选择性的加载一个布局文件(decor.addView()这种方式,新版本在onResourcesLoaded这个方法中),比如这个:
screen_custom_title.xml

<?xml version="1.0" encoding="utf-8"?>
<!--
This is a custom layout for a screen.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:fitsSystemWindows="true">
    <!-- Popout bar for action modes -->
    <ViewStub android:id="@+id/action_mode_bar_stub"
              android:inflatedId="@+id/action_mode_bar"
              android:layout="@layout/action_mode_bar"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:theme="?attr/actionBarTheme" />
    <FrameLayout android:id="@android:id/title_container" 
        android:layout_width="match_parent" 
        android:layout_height="?android:attr/windowTitleSize"
        android:transitionName="android:title"
        style="?android:attr/windowTitleBackgroundStyle">
    </FrameLayout>
    <FrameLayout android:id="@android:id/content"
        android:layout_width="match_parent" 
        android:layout_height="0dip"
        android:layout_weight="1"
        android:foregroundGravity="fill_horizontal|top"
        android:foreground="?android:attr/windowContentOverlay" />
</LinearLayout>

我们通过setContentView方法设置的布局文件就是作为这个id为content的子元素的

第二次编辑(北邮)

FLAG_NOT_FOCUSABLE 不需要获取焦点,也不需要接收各种输入事件,此标记会同时启用FLAG_NOT_TOUCH_MODAL,最终事件会直接传递给下层具有焦点的Window

FLAG_NOT_TOUCH_MODAL 系统会将当前Window区域以外的单击事件传递给下面的Window,当前Window区域以内的单击事件则自己处理

FLAG_NOT_TOUCH_MODAL 可以让Window显示在锁屏的界面上

如果要创建系统Window,需要声明权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
一般可以使用:TYPE_SYSTEM_OVERLAY或者TYPE_SYSTEM_ERROR

Activity与Window

问题:

1、Activity与Window有什么关系,在Activity初始化的过程中Window是什么时候被创建的?
2、Activity的setContentView(View view)方法,最终是将view设置给了谁,这个view是如何被添加到Window中的,是什么时候添加的?

wm.addView(decor, l);

Dialog与Window

Dialog属于子Window,所以在初始化Dialog的时候传入的context对象必须是Activity的,如果是Application的则会报错。

可以调用Dialog的getWindow方法得到Window对象,然后设置Window对象的type为系统类型,这种情况下就可以使用Application的context了(前提是需要声明权限)

Toast与Window

Toast.makeText(this,"显示的内容",Toast.LENGTH_LONG).show();

是因为makeText方法内部会加载一个默认的布局,最终都是将布局赋值给Toast的mNextView成员变量

Activity、Dialog中Window对象的作用貌似就是通过Window对象的setContentView方法将布局设置给DecorView对象,然后在合适的时机通过WindowManager对象的addView方法将DecorView添加进Window,Toast中最后通过WindowManager添加的view是mNextView

上一篇 下一篇

猜你喜欢

热点阅读