模仿Toast自定义悬浮窗口

   private boolean mShowTips = false;
   private static final int FLOAT_VIEW_DISPLAY_TIME = 2000; 


   private void showTips(int resid) {
        if (mShowTips) {
            Log.w(TAG,"The floating window is showing, stop showing another one.");
            return;
        }

        //Android toast is not able to be shown on top of notifications, so
        //implement a floating window to replace it.
        LayoutInflater inflate = (LayoutInflater)
                ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View floatView = inflate.inflate(
                com.android.internal.R.layout.transient_notification, null);
        final TextView textView = (TextView) floatView
               .findViewById(com.android.internal.R.id.message);

        textView.setText(ctx.getString(resid));
        final WindowManager windowManager = (WindowManager) ctx
                .getSystemService(ctx.WINDOW_SERVICE);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.type = WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL;
        params.format = PixelFormat.TRANSLUCENT;
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.y = windowManager.getDefaultDisplay().getHeight()/3;
        params.windowAnimations = com.android.internal.R.style.Animation_Toast;
        windowManager.addView(floatView, params);
        mShowTips = true;

        //close notification in two seconds
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
            	windowManager.removeView(floatView);
                mShowTips = false;
            }
        }, FLOAT_VIEW_DISPLAY_TIME);
     }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值