如果每个界面都要显示悬浮窗,则把这段代码写在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("我是悬浮窗");
});