【问题标题】:About the position of the added view after calling WindowManager.addView()关于调用WindowManager.addView()后添加视图的位置
【发布时间】:2017-02-15 08:44:55
【问题描述】:

问题来了~我尝试通过调用WindowManager.addView()方法给窗口添加一个ImageView。但是我发现 ImageView 在屏幕的中心,而不是左上角。只有当我将 WindowManager.LayoutParams 的重力属性设置为 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的源码中,我发现只有设置gravity的时候才会考虑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 的代码)。

任何提示将不胜感激。

【问题讨论】:

  • 很可能是TYPE_SYSTEM_ALERT 导致
  • 但是你能找到处理这个属性(重力、x、y、类型等)的源代码吗?看源码的时候迷路了
  • 试过TYPE_PHONE?喜欢this
  • 我试过了,图像仍然在屏幕的中心,,,

标签: android android-windowmanager


【解决方案1】:

我猜这会导致:

WindowManager.LayoutParams.TYPE_SYSTEM_ALERT

【讨论】:

  • 我同意你的说法,但我找不到对应的源代码
猜你喜欢
  • 2012-03-29
  • 2018-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-29
  • 2020-09-06
  • 1970-01-01
相关资源
最近更新 更多