这是问题〜我试图通过调用window manager.addview()方法将imageView添加到窗口中。但是我发现图像视图在屏幕的中心,而不是左上角。仅当我将WindowManager的重力属性设置为重力时,它才会出现在左上角。重力属性重力的默认值。我研究了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;
但是这还不够
任何提示都将不胜感激。
我猜这会导致:
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT