您的位置:首页 > 移动开发 > Android开发

Android中 Window 、WindowManager、WindowManager.LayoutParams 相关内容

2016-01-12 01:51 435 查看
声明本文内容部分参考各大神的贴,统一感谢,有错勿喷~·

还是习惯,罗列内容要点:

一、Window 、WindowManager、WindowManager.LayoutParams接口、类的结构介绍

二、Window 、WindowManager的关系。

三、讲解WindowManager.LayoutParams的参数

一、首先介绍一下上述接口、类的结构

1)抽象类:window

  定义窗口样式和行为的抽象基类,用于作为顶层的view加到windowManager中。唯一实现了这个抽象类的是PhoneWindow,实例化PhoneWindow需要一个窗口

  public abstract class Window

  其中有一个很重要的内部类

  private class LocalWindowManager extends WindowManagerImpl.CompatModeWrapper{…};

2)抽象类:viewGroup

  包含其他view的容器,layouts和view 容器的基类。

  public abstract class ViewGroup extends View implements ViewParent, ViewManager

3)相关接口:ViewParent

  定义了一个view parent 的要负责的功能以及view和parent view之间的关联

    public interface ViewParent {

         public void requestLayout();

        public void createContextMenu(ContextMenu menu);

        public void bringChildToFront(View child);

        …..

    }

4)相关接口:viewManager

  用来添加和移除activity中的view的接口

public interface ViewManager

{

public void addView(View view, ViewGroup.LayoutParams params);

public void updateViewLayout(View view, ViewGroup.LayoutParams params);

public void removeView(View view);

}

5)接口:windowManager

  用来在应用与window之间的管理接口,管理窗口顺序,消息等

public interface WindowManager extends android.view.ViewManager

6)WindowManager.LayoutParams

WindowManager.LayoutParams 是 WindowManager 接口的嵌套类;它继承于 ViewGroup.LayoutParams; 它用于向WindowManager描述Window的管理策略。

java.lang.Object

? android.view.ViewGroup.LayoutParams

? android.view.WindowManager.LayoutParams

二.他们之间的内在关系。

1)Window:

Window是android中的窗口,表示顶级窗口的意思,也就是主窗口,它有两个实现类,PhoneWindow和MidWindow,我们一般的activity对应的主要是PhoneWindow,在activity中经常使用的setContentView等方法也是在这个里面实现的。每个主窗口中都有一个View,称之为DecorView,是主窗口中的顶级view(实际上就是ViewGroup),在View中有两个成员变量叫做mParent、mChildren,它是用来管理view的上下级关系的。而ViewGroup是对一组View的管理。因此,在ViewGroup中建立了所有view的关系网。而最终ViewGroup附属在主窗口上。这样就很容易在窗口中通过findViewById找到具体的View了。view中的事件处理也是根据这个路径来处理的。window的获取是通过下面方法获取的。

Window mWindow = PolicyManager.makeNewWindow(this);

Window(主窗口) =》DecorView(顶级View,也是ViewGroup) =》ViewGroup…..ViewGroup =》View….

2)WindowManager:

WindowManager主要用来管理窗口的一些状态、属性、view增加、删除、更新、窗口顺序、消息收集和处理等。

通过Context.getSystemService(Context.WINDOW_SERVICE)的方式可以获得WindowManager的实例.

WindowManager继承自ViewManager,里面涉及到窗口管理的三个重要方法,分别是:

* addView();

* updateViewLayout();

* removeView();

三.讲解WindowManager.LayoutParams的参数

1)WindowManager.LayoutParams的应用实例,windowManager中添加一个悬浮框的方式,代码如下:

WindowManager.LayoutParams params = new LayoutParams();

params.width = width;

params.height = height;

params.format = PixelFormat.TRANSLUCENT;

params.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;

params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;//后面窗口仍然可以处理点设备事件

params.setTitle(“Toast”);

params.gravity = gravity;

params.windowAnimations = styleAnimations;

WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);

TextView float = new TextView(context);

float.settext(“this is a float window “);

windowManager.addView(view, this.mLayoutParams);

2)参数type:

该系列主要用于表示window的类型。我们可以通过WindowManager.LayoutParams的type变量直接进行设置.

window 的类型被分为了3大类:

Application windows: (ranging from FIRST_APPLICATION_WINDOW to LAST_APPLICATION_WINDOW) are normal top-level application windows. For these types of windows, the token must be set to the token of the activity they are a part of (this will normally be done for you if token is null).

Sub-windows: (ranging from FIRST_SUB_WINDOW to LAST_SUB_WINDOW) are associated with another top-level window. For these types of windows, the token must be the token of the window it is attached to.

System windows: (ranging from FIRST_SYSTEM_WINDOW to LAST_SYSTEM_WINDOW) are special types of windows for use by the system for specific purposes. They should not normally be used by applications, and a special permission is required to use them.

3)参数flag:

该系列主要用于对Window的flag进行设置。设置Window的flag,可以直接对Window的getAttributes()得到其 WindowManager.LayoutParams对象,然后直接对它flag变量操作。也可以Window的addFlags(int flags)方法,setFlags(int flags, int mask)方法,clearFlags(int flags)方法进行操作。

比如设置全屏:

Window window = getWindow();

WindowManager.LayoutParams winParams = win.getAttributes();

winParams.flags=winParams.flags|WindowManager.LayoutParams.FLAG_FULLSCREEN;



window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);



window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

取消全屏:

Window window = getWindow();

winParams.flags=winParams.flags&~WindowManager.LayoutParams.FLAG_FULLSCREEN;



window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);



window.setFlags(0, WindowManager.LayoutParams.FLAG_FULLSCREEN);

4)参数windowAnimation:

很多人设置了windowAnimation,但是没有动画效果呢?

这里一定要注意,这里需要使用style,在style中添加如下:

@anim/sanqiwan_toast_slide_top_enter

@anim/sanqiwan_toast_slide_top_exit

然后将style的resourceId赋给params.windowAnimation,如果是将动画的resourceId赋值给params.windowAnimation,死也看得不到动画效果滴。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: