Demo entry 6656210

wm

   

Submitted by hh on Oct 30, 2017 at 08:29
Language: Java. Code size: 1.6 kB.

private Button  mFloatButton ;
    private WindowManager.LayoutParams mLayoutParams;
    private  WindowManager  mWindowManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_main);
        mFloatButton = new Button(this);
        mFloatButton.setText("button");
        mLayoutParams = new WindowManager.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,
                0,0,
                PixelFormat.TRANSLUCENT
        );
        mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL // Window不需要获取焦点,也不需要输入事件,使最终事件直接传递给下层
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE//在当前Window区域外的点击单件传递给底层Window,当前Window区域以内的点击事件则自己处理。默认应该开开,否则其他Window无法收到单击事件
                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;//让Window显示到锁屏界面
        mLayoutParams.gravity = Gravity.LEFT|Gravity.TOP;
        mLayoutParams.x=100;
        mLayoutParams.y=300;
        mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
        mWindowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
        mWindowManager.addView(mFloatButton,mLayoutParams);
        /*
        * Caused by: android.view.WindowManager$InvalidDisplayException: Unable to add window android.view.ViewRootImpl$W@613461a -- the specified window type 0 is not valid
        * android.permission.SYSTEM_ALERT_WINDOW :     <permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
        *
        * */

    }

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).