Android Can';t单击作为覆盖的视图下的按钮
我正在开发一个应用程序,它在屏幕上叠加了一个视图,作为屏幕上的一种色调。我已经得到了我想要的颜色的视图,我可以看到它后面的按钮。。问题是我无法单击它们!:( 以下是该应用程序在屏幕上实现的效果(这是我想要的外观):Android Can';t单击作为覆盖的视图下的按钮,android,overlay,Android,Overlay,我正在开发一个应用程序,它在屏幕上叠加了一个视图,作为屏幕上的一种色调。我已经得到了我想要的颜色的视图,我可以看到它后面的按钮。。问题是我无法单击它们!:( 以下是该应用程序在屏幕上实现的效果(这是我想要的外观): import android.app.Service; import android.content.Intent; import android.graphics.Color; import android.graphics.PixelFormat; import android.
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.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);
我的代码:
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.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);
干杯!将WindowManager布局参数更改为:
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.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:)将WindowManager布局参数更改为:
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.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:)我不确定,但是您是否已经尝试覆盖'onInterceptTouchEvent'以返回'filter'-视图的false?在layoutparams中,您应该包括“FLAG_not_TOUCHABLE”。@Aleksandrmedvedevev该视图(超类)没有名为
onInterceptTouchEvent
的方法@DeeV!非常感谢。这不起作用Marshmallow我不确定,但您是否已经尝试覆盖“onInterceptTouchEvent”以返回“filter”视图的false?在layoutparams中,您应该包括“FLAG\u not\u TOUCHABLE”。@AleksandrMedvedev视图(超类)没有名为onInterceptTouchEvent
@DeeV brilliant fixed!非常感谢。这不适用于Marshmallow在我创建的新AVD上,覆盖右侧被切断,这是通过将第一个参数更改为WindowManager.LayoutParams.MATCH\u PARENT
而不是WindowManager.LayoutParams.WRAP\u CONTENT
来修复的。在新AVD上,我创建的覆盖右侧被切断,通过将第一个参数更改为WindowManager.LayoutParams.MATCH_PARENT
而不是WindowManager.LayoutParams.WRAP_CONTENT
,可以解决此问题。