在否决重复投票之前,请检查我到目前为止所做的尝试。
我试图在外部触摸时关闭弹出窗口。我已经尝试了所有可用的解决方案,但都没有用。
第一次尝试:
pwindo.setBackgroundDrawable(new BitmapDrawable());
pwindo.setFocusable(true);
pwindo.setOutsideTouchable(true);
第二次尝试:
pwindo.setBackgroundDrawable(new ColorDrawable());
pwindo.setFocusable(true);
pwindo.setOutsideTouchable(true);
第三次尝试:
pwindo.setBackgroundDrawable(new ColorDrawable());
pwindo.setFocusable(true);
pwindo.setOutsideTouchable(false);
第四次尝试:
pwindo.setFocusable(true);
pwindo.setOutsideTouchable(false);
第五次尝试:
pwindo.setOutsideTouchable(true);
第六次尝试:
pwindo.setOutsideTouchable(false);
第七次尝试:
pwindo.setFocusable(true);
没有任何效果。
更新:
public void addFlyout()
{
try {
LayoutInflater inflater = TabActivity.this.getLayoutInflater();
Display display = getWindowManager().getDefaultDisplay();
Method mGetRawH = Display.class.getMethod("getRawHeight");
Method mGetRawW = Display.class.getMethod("getRawWidth");
int rawHeight = (Integer) mGetRawH.invoke(display);
View view = inflater.inflate(R.layout.flyout_list_layout,
(ViewGroup) findViewById(R.id.flyoutLayoutList));
Dialog dialog = new Dialog(TabActivity.this);
pwindo = new PopupWindow(view, 340, rawHeight- actionBar.getHeight(), true);
pwindo.showAtLocation(view, Gravity.RIGHT | Gravity.TOP, 0, actionBar.getHeight());
pwindo.setBackgroundDrawable(new ColorDrawable(Color.RED));
// pwindo.setTouchable(true);
pwindo.setOutsideTouchable(true);
pwindo.setTouchInterceptor(new View.OnTouchListener() {
@Override public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_OUTSIDE)
{
pwindo.dismiss();
//Log.e(TAG, "some event happened outside window: action outside");
return true;
}
// Log.e(TAG, "some event happened outside window");
return false;
}
});
listFlyout = (ListView) view.findViewById(R.id.list_slideList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(TabActivity.this, R.layout.drawer_item, R.id.content, tabs);
listFlyout.setAdapter(adapter);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
和
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
try {
if(tab.getPosition() == 4)
{
addFlyout();
}
else
mViewPager.setCurrentItem(tab.getPosition());
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
最佳答案
在 PopupWindow
上设置背景可确保在其范围外单击可将其关闭。
showAtLocation(...)
:
public void showAtLocation(IBinder token, int gravity, int x, int y) {
....
preparePopup(p);
....
}
preparePopup(...)
检查是否设置了背景。如果是,您传递的内容 View (在构造函数中)将添加到自定义 FrameLayout
。然后在这个自定义 FrameLayout
上设置背景:
if (mBackground != null) {
....
// when a background is available, we embed the content view
// within another view that owns the background drawable
PopupViewContainer popupViewContainer = new PopupViewContainer(mContext);
PopupViewContainer.LayoutParams listParams = new PopupViewContainer.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, height
);
popupViewContainer.setBackground(mBackground);
popupViewContainer.addView(mContentView, listParams);
....
} else {
....
}
由于您正在调用 setBackgroundDrawable(...)
在 showAtLocation(...)
之后,preparePopup(... )
不会将内容 View 放在 PopupViewContainer
中。
PopupViewContainer
还覆盖了 onTouchEvent(...)
以提供“dismissed-when-touched-outside”行为:
@Override
public boolean onTouchEvent(MotionEvent event) {
final int x = (int) event.getX();
final int y = (int) event.getY();
if ((event.getAction() == MotionEvent.ACTION_DOWN)
&& ((x < 0) || (x >= getWidth()) || (y < 0) || (y >= getHeight()))) {
dismiss();
return true;
} else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
dismiss();
return true;
} else {
return super.onTouchEvent(event);
}
}
显然,您只需要提供背景,并在调用 showAtLocation(...)
之前提供它。
关于android - 弹出窗口在外部触摸时关闭不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29165591/