无法单击充当叠加层的视图下的按钮
Can't click buttons under a View acting as an overlay
我正在开发一个应用程序,它有一个覆盖在屏幕上的视图作为屏幕上的色调。我已经得到了我想要的颜色的视图,我可以看到它后面的按钮..问题是我不能点击它们! :(
这是应用程序在屏幕上实现的效果(这就是我想要的样子):
我的代码:
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
}
}
干杯!
已通过将 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 :)
我正在开发一个应用程序,它有一个覆盖在屏幕上的视图作为屏幕上的色调。我已经得到了我想要的颜色的视图,我可以看到它后面的按钮..问题是我不能点击它们! :(
这是应用程序在屏幕上实现的效果(这就是我想要的样子):
我的代码:
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
}
}
干杯!
已通过将 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 :)