我有一个动态创建的覆盖窗口,它在窗口的某个布局上有一个 OnTouchListener。当我使用 OnTouchListener 检测到触摸时,我希望删除此覆盖窗口。这个覆盖是动态创建的,不是活动的一部分,因此不遵循活动生命周期,因此我不能调用finish()。那我该如何删除视图呢?下面的代码片段是我创建视图的位置,以及我的 OnTouchListener 所在的位置。我应该如何删除覆盖?
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
flags,
PixelFormat.TRANSLUCENT);
LinearLayout l = new LinearLayout(service);
RelativeLayout mainView = new RelativeLayout(service);
RelativeLayout.LayoutParams param_one = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
param_one.leftMargin=0;
param_one.topMargin=0;
l.setTag("first_tag");
l.setOrientation(LinearLayout.VERTICAL);
l.setBackgroundColor(0x00ffffff);
EditText second_test = new EditText(service);
second_test.setBackgroundColor(Color.parseColor("#0b0b0b")); //TODO: not perfect color
second_test.setTextColor(Color.LTGRAY);
second_test.setWidth(440);
second_test.setTag("second_tag");
second_test.setTextSize(20);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
second_test.setLayoutParams(llp);
l.addView(second_test);
mainView.addView(l, param_one);
LinearLayout bottomLayout = new LinearLayout(service);
RelativeLayout.LayoutParams bottomparam = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
bottomparam.width=700;
bottomparam.height=120;
bottomparam.leftMargin=10;
bottomparam.topMargin=260;
bottomLayout.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d(TAG, "onTouch");
// Remove the overlay here
return true;
}
});
mainView.addView(bottomLayout, bottomparam);
我尝试调用 WindowManager.removeView(thisView),但在 onCreate() 之前,诸如 WindowManager 之类的系统服务对活动不可用。