如果每个界面都要显示悬浮窗,则把这段代码写在BaseActivity即可,如果只是在某个界面显示,直接在该Activity写就可以了,或是直接在布局xml文件写。
FrameLayout rootView = (FrameLayout) getWindow().getDecorView();
FrameLayout.LayoutParams textParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
textParams.gravity = Gravity.RIGHT | Gravity.BOTTOM;
textParams.bottomMargin = DensityUtils.dp2px(this, 60);
textParams.rightMargin = DensityUtils.dp2px(this, 5);
FloatingActionButton actionButton = new FloatingActionButton(this);
actionButton.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.icon_float_view));
ColorStateList colorStateList = ContextCompat.getColorStateList(this, R.color.color999999);
actionButton.setBackgroundTintMode(PorterDuff.Mode.SRC_ATOP);
actionButton.setBackgroundTintList(colorStateList);
actionButton.setLayoutParams(textParams);
actionButton.setOnClickListener(v -> {
ToastUtils.showShort("我是悬浮窗");
});