关于调用WindowManager.addView()

时间:2017-02-15 08:44:56

标签: android android-windowmanager

这是问题〜我试图通过调用WindowManager.addView()方法将ImageView添加到窗口中。但我发现ImageView位于屏幕的中心,而不是左上角。只有当我将WindowManager.LayoutParams的gravity属性设置为Gravity.LEFT | Gravity.TOP时,它才出现在左上角,为什么?重力属性Gravity.CENTER的默认值是多少?我查看了android源代码,但迷路了。

    ImageView img = new ImageView(this);
    img.setImageResource(android.R.drawable.ic_delete);
    WindowManager manager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(60,60,WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT);
    //params.gravity= Gravity.LEFT|Gravity.TOP;
    manager.addView(img, params);

在WindowManager.LayoutParams的源代码中,我发现只有在设置了引力时,才会考虑x和y属性:

    /**
     * X position for this window.  With the default gravity it is ignored.
     * When using {@link Gravity#LEFT} or {@link Gravity#START} or {@link Gravity#RIGHT} or
     * {@link Gravity#END} it provides an offset from the given edge.
     */
    @ViewDebug.ExportedProperty
    public int x;

    /**
     * Y position for this window.  With the default gravity it is ignored.
     * When using {@link Gravity#TOP} or {@link Gravity#BOTTOM} it provides
     * an offset from the given edge.
     */
    @ViewDebug.ExportedProperty
    public int y;

然而这还不够,我想知道未设置重力时会发生什么(更具体地说,我想找到绘制ImageView的代码)。

任何提示都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

我想这会导致:

WindowManager.LayoutParams.TYPE_SYSTEM_ALERT