• Android中悬浮窗口


    调用WindowManager,并设置WindowManager.LayoutParams的相关属性,通过WindowManager的addView方法创建View,这样产生出来的View根据WindowManager.LayoutParams属性不同,效果也就不同了。比如创建系统顶级窗口,实现悬浮窗口效果!

    WindowManager的方法很简单,基本用到的就三个addView,removeView,updateViewLayout。

    而WindowManager.LayoutParams的属性就多了,非常丰富,具体请查看SDK文档。这里给出Android中的WindowManager.java源码,可以具体看一下

    下面是简单示例代码:
     

    public class myFloatView extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Button bb=new Button(getApplicationContext());
            WindowManager wm=(WindowManager)getApplicationContext().getSystemService("window");
            WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
     
            /**
             *以下都是WindowManager.LayoutParams的相关属性
             * 具体用途请参考SDK文档
             */
            wmParams.type=2002;   //这里是关键,你也可以试试2003
            wmParams.format=1;
             /**
             *这里的flags也很关键
             *代码实际是wmParams.flags |= FLAG_NOT_FOCUSABLE;
             *40的由来是wmParams的默认属性(32)+ FLAG_NOT_FOCUSABLE(8)
             */
            wmParams.flags=40;     
            wmParams.width=40;
            wmParams.height=40;
            wm.addView(bb, wmParams);  //创建View
        }
    }

    别忘了在AndroidManifest.xml中添加权限:

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

    PS:这里举例说明一下type的值的意思:

            /**
             * Window type: phone.  These are non-application windows providing
             * user interaction with the phone (in particular incoming calls).
             * These windows are normally placed above all applications, but behind
             * the status bar.
             */
            public static final int TYPE_PHONE              = FIRST_SYSTEM_WINDOW+2;
     
            /**
             * Window type: system window, such as low power alert. These windows
             * are always on top of application windows.
             */
            public static final int TYPE_SYSTEM_ALERT       = FIRST_SYSTEM_WINDOW+3;

    这个FIRST_SYSTEM_WINDOW的值就是2000。2003和2002的区别就在于2003类型的View比2002类型的还要top,能显示在系统下拉状态栏之上!

  • 相关阅读:
    iOS构建流畅的交互界面--卡顿产生的原因
    iOS-tableViewCell重用机制带来的问题及解决
    完全背包
    poj1384
    RunLoop的使用--AFNetworking的网络线程实现+tableView延迟加载图片+App崩溃后立即重启
    poj 3624
    01背包
    poj1416
    java中 File文件常用操作方法的汇总
    线程中涉及到的知识点总结
  • 原文地址:https://www.cnblogs.com/GnagWang/p/1949569.html
Copyright © 2020-2023  润新知