获取 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(安卓)Api WindowManager.LayoutParams
  3. Android(安卓): upload Image using MultiPartEntityBuilder
  4. 【Android问题】解决 Android(安卓)SDK下载和更新失败“Connecti
  5. Android中的searchView去掉前面的搜索圈
  6. Android(安卓)SDK下载和更新失败的解决方法
  7. Android修改AlertDialog的背景颜色
  8. 输入法弹出后,不影响activity原有布局(android:windowSoftInputMo
  9. android framework java层服务 分析

随机推荐

  1. android uid gid
  2. android中获取手机短信,删除短信功能
  3. Aandroid 关于处理个推透传消息
  4. Java W3C 生成 XML
  5. Android与服务器http连接模块代码
  6. android--widget之menu
  7. HandlerTest
  8. MediaTest
  9. 仿微信运动步数折线统计图
  10. Android(安卓)Development