Android WindowManager.LayoutParams y offset

时间:2015-09-26 12:08:38

标签: android layoutparams

我想从屏幕顶部以编程方式在全屏幕上绘制内容。但是,始终存在y轴偏移。它似乎是状态(标题)栏的高度。

这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                    | WindowManager.LayoutParams.FLAG_FULLSCREEN,
            PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.LEFT | Gravity.TOP;
    params.x = 0;
    params.y = 0;
    params.width = 200;
    params.height = 300;

    GradientDrawable gd = new GradientDrawable();
    gd.setShape(GradientDrawable.RECTANGLE);
    gd.setColor(Settings.highlightColor);
    ImageView iv = new ImageView(this);
    iv.setImageDrawable(gd);
    WindowManager windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
    windowManager.addView(iv, params);

我已经在AndroidManifest.xml中设置了全屏:

android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"

这是我得到的:

Result

2 个答案:

答案 0 :(得分:2)

使用此:

WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;

答案 1 :(得分:0)

尝试将FLAG_TRANSLUCENT_STATUS添加到LayoutParams

paramsFlags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;