无法单击视图作为叠加层下的按钮

时间:2016-01-20 20:34:04

标签: android overlay

我正在开发一个应用程序,它在屏幕上叠加了一个视图,作为屏幕上的颜色色调。我已经看到了我想要的颜色,我可以看到它背后的按钮..问题是我无法点击它们! :(

以下是该应用在屏幕上取得的成就(这就是我希望它的外观):

Notice the yellow tinge

我的代码:

import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

public class OverlayService extends Service {
    private WindowManager windowManager;
    private View filter;

    @Override
    public IBinder onBind(Intent intent) {
        // Not used
        return null;
    }

    @Override public void onCreate() {
    super.onCreate();

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    filter = new View(this); // Create a new view
    float alpha = (float) 0.8; // Create alpha variable
    filter.setAlpha(alpha); // Set alpha (this doesn't seem to do anything)
    filter.setBackgroundColor(Color.YELLOW); // Set the background colour to yellow
    filter.getBackground().setAlpha(80); // Set the background's alpha (this is the call that works!)

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.TYPE_PHONE,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
        PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.TOP | Gravity.LEFT;
    params.x = 0;
    params.y = 100;

    windowManager.addView(filter, params);
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    if (filter != null) windowManager.removeView(filter); // If the filter exists, remove it
  }

}

干杯!

1 个答案:

答案 0 :(得分:0)

通过将WindowManager布局参数更改为:

来修复
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.TYPE_PHONE,
        WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, // This line changed!
        PixelFormat.TRANSLUCENT);

感谢@DeeV:)