【问题标题】:Android popup window dismissal when touching outside the popup [duplicate]在弹出窗口之外触摸时Android弹出窗口解除[重复]
【发布时间】:2014-12-27 03:18:31
【问题描述】:

我在 Android 中遇到了弹出窗口的问题。我想要做的是当用户第一次点击按钮时,会显示弹出窗口。如果显示弹出窗口并且用户再次单击该按钮,则弹出窗口将被隐藏。这是弹出窗口的 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llAttendeeList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 
android:background="#000000"
>

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="HELLO" />

</LinearLayout>

以及点击按钮的代码,它会执行这个方法:

private void openPopUp(){
    LayoutInflater layoutInflater = (LayoutInflater) getActivity()
            .getBaseContext().getSystemService(
                    context.LAYOUT_INFLATER_SERVICE);
    View popupView = layoutInflater.inflate(
            R.layout.event_attendee_pop, null);
    llAttendeeList = (LinearLayout) popupView
            .findViewById(R.id.llAttendeeList);
    final PopupWindow popupWindow = new PopupWindow(popupView,
            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    popupWindow.setOutsideTouchable(true);

    popupWindow.setTouchInterceptor(new OnTouchListener() { 
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_OUTSIDE) 
                    {
                        popupWindow.dismiss();
                        return true;
                    }
                    return false;
                }
            });
    popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
}

但是,当用户第一次点击按钮时,弹出窗口确实出现了。但是当我在弹出窗口之外单击时,它不会关闭。有什么想法吗?

提前致谢。

【问题讨论】:

    标签: java android popupwindow


    【解决方案1】:

    我发现我必须将其设置为弹出窗口:

    popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable());
    

    【讨论】:

      【解决方案2】:

      添加popupWindow.setContentView(popupView) 并重试

      【讨论】:

      • 你有什么想法如何在操作栏下方设置一个带有 marginTop 的弹出窗口?
      • 有什么想法吗?我无法将其设置为我想要的位置
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      • 1970-01-01
      • 2022-12-31
      • 2018-04-03
      • 2015-05-27
      相关资源
      最近更新 更多