获取 WindowManager

mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

初始化 Params

mLayoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,    WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE,    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);

设置 Params 的其他属性

// Set gravity.mLayoutParams.gravity = Gravity.TOP | Gravity.START;// Adjust origin position by base point.mLayoutParams.x = 0;mLayoutParams.y = 0;// Adjust origin position by margin.mLayoutParams.verticalMargin = LAYOUT_PARAMS_VERTICAL_MARGIN;mLayoutParams.horizontalMargin = LAYOUT_PARAMS_HORIZONTAL_MARGIN;// Set layout height with constant.mLayoutParams.height = LAYOUT_PARAMS_HEIGHT;// The scale is decided by origin icon scale.mLayoutParams.width = LAYOUT_PARAMS_HEIGHT * LAYOUT_PARAMS_SCALE;

实例化 View

mFloatingView = new ImageView(context);mFloatingView.setImageResource(iconResId);mFloatingView.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View view) {        // Your code here.    }});

显示悬浮窗口

mWindowManager.addView(mFloatingView, mLayoutParams);

消除悬浮窗口

if (mFloatingView != null && mFloatingView.isShown()) {    mWindowManager.removeView(mFloatingView);}

完整代码

/** * Created by YuYi App Dev on 2016/8/22. * To create a floating button. */public class FloatingBackButton {    private Context context;    private int iconResId;    private Class<?> cls;    private ImageView mFloatingView;    private WindowManager mWindowManager;    private WindowManager.LayoutParams mLayoutParams;    /**     * Constructor function.     *     * @param context   the context     * @param iconResId the pic needed to be displayed     * @param cls       the class needed to be loaded when button pressed     */    public FloatingBackButton(Context context, int iconResId, Class<?> cls) {        this.context = context;        this.iconResId = iconResId;        this.cls = cls;        initResources();    }    private void initResources() {        // Set as local var to save res.        final int LAYOUT_PARAMS_HEIGHT = 120;        final int LAYOUT_PARAMS_SCALE = 2;        final int LAYOUT_PARAMS_VERTICAL_MARGIN = 0;        final int LAYOUT_PARAMS_HORIZONTAL_MARGIN = 0;        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);        mLayoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,                WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE,                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);        // Set gravity.        mLayoutParams.gravity = Gravity.TOP | Gravity.START;        // Adjust origin position by base point.        mLayoutParams.x = 0;        mLayoutParams.y = 0;        // Adjust origin position by margin.        mLayoutParams.verticalMargin = LAYOUT_PARAMS_VERTICAL_MARGIN;        mLayoutParams.horizontalMargin = LAYOUT_PARAMS_HORIZONTAL_MARGIN;        // Set layout height with constant.        mLayoutParams.height = LAYOUT_PARAMS_HEIGHT;        // The scale is decided by origin icon scale.        mLayoutParams.width = LAYOUT_PARAMS_HEIGHT * LAYOUT_PARAMS_SCALE;        // Must initiating when attaching, or will cause crash.        mFloatingView = new ImageView(context);        mFloatingView.setImageResource(iconResId);        mFloatingView.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                Intent intent = new Intent(context, cls);                context.startActivity(intent);                if (mFloatingView != null && mFloatingView.isShown()) {                    mWindowManager.removeView(mFloatingView);                }            }        });    }    public void show() {        mWindowManager.addView(mFloatingView, mLayoutParams);    }}

更多相关文章

  1. android设置保持窗口常亮
  2. Android 之 Window、WindowManager 与窗口管理
  3. [zz] Android 初始化语言(Android init Language翻译)
  4. android的窗口机制分析------事件处理
  5. Android之SurfaceView窗口/全屏播放
  6. Android - Android Studio 的 Preview窗口

随机推荐

  1. android简繁体相互转换
  2. android studio最常用的快捷键
  3. ConfigurationTest以及横竖屏切换及2.3与
  4. Android中的Uri和ContentProvider
  5. 关于ScrollView嵌套RecyclerView时Recycl
  6. App版本更新时对SQLite数据库升级或者降
  7. Android(安卓)5种倒计时的实现
  8. Android(安卓)JNI开发进阶篇
  9. android_qqApk反编译/android小工具/ddms
  10. 简析Android多种AlertDialog对话框效果