Open a PopupWindow and let the outsides still touchable

六月ゝ 毕业季﹏ 提交于 2019-12-07 03:16:28

问题


How to open a PopupWindow on Android and let all the others components touchable without dismiss the PopupWindow?

This is how it's created:

public class DynamicPopup {
    private final PopupWindow window;
    private final RectF rect;
    private final View parent;
    private final RichPageView view;

    public DynamicPopup(Context context, RichPage page, RectF rectF, View parent) {
        this.parent = parent;
        rect = rectF;

        window = new PopupWindow(context);

        window.setBackgroundDrawable(new BitmapDrawable());
        window.setWidth((int) rect.width());
        window.setHeight((int) rect.height());
        window.setTouchable(true);
        window.setFocusable(true);
        window.setOutsideTouchable(true);

        view = new RichPageView(context, page, false);
        window.setContentView(view);

        view.setOnCloseListener(new Listener(){
            @Override
            public void onAction() {
                window.dismiss();
            }
        });


    }

    public void show() {
        window.showAtLocation(parent, Gravity.NO_GRAVITY, (int) rect.left, (int) rect.top);
    }
}

回答1:


As per javadocs

Controls whether the pop-up will be informed of touch events outside of its window. This only makes sense for pop-ups that are touchable but not focusable

So your line

 window.setFocusable(true);

causes the method setOutsideTouchable() to do nothing.




回答2:


just like the ernazm say

As per javadocs

Controls whether the pop-up will be informed of touch events outside of its window. This only makes sense for pop-ups that are touchable but not focusable

and it's work on

window.setTouchable(true);
window.setFocusable(false);
window.setOutsideTouchable(false);

When window touchalbe is true, focusable is false, setOutsideTouchable() is work, if setOutsideTouchable(true), touch outside of popupwindow will dismiss, otherwise the outside of popupwindows still can be touchable without dismiss.




回答3:


For that you have to made the Custom Popup window. In that u have to call the another layout. So in this way You can access the other component also. It just the One type of the Layout. But you can set its appearence as Popup Window.

<!-- POPUP MENU -->
    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/popup_window"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/fullwindowborderforpopup"
        >



        <LinearLayout

            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"

            android:padding="1px"
            android:layout_marginTop="15dip"
            android:layout_marginLeft="20dip"
            android:layout_marginRight="20dip"
            android:layout_marginBottom="10dip"
            android:background="@drawable/borderforpopup"
            >
            <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical" 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:layout_marginTop="5dip"
                android:layout_marginLeft="3dip"
                android:layout_marginRight="3dip"
                android:layout_marginBottom="3dip">

                <TextView 
                    android:id="@+id/tital_of_popup"
                    android:gravity="center_vertical|center_horizontal" 
                    android:layout_height="wrap_content" 
                    android:layout_width="wrap_content"
                    android:text="Event Registration"
                    android:textStyle="bold"
                    android:textColor="#ffffff" 
                    />
            </LinearLayout>

            <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical" 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:layout_marginTop="5dip"
                android:layout_marginLeft="3dip"
                android:layout_marginRight="3dip"
                android:layout_marginBottom="3dip"
            >

                <TextView 
                    android:id="@+id/message_of_popup"
                    android:gravity="center_vertical|center_horizontal" 
                    android:layout_height="wrap_content" 
                    android:text="Please fill all the data" 
                    android:layout_width="wrap_content"
                    android:textStyle="normal"
                    android:textColor="#ffffff" />
        </LinearLayout>
        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:layout_marginTop="5dip"
            android:layout_marginLeft="6dip"
            android:layout_marginRight="6dip"
            android:layout_marginBottom="9dip"
                >


             <Button 
                android:layout_height="fill_parent" 
                android:id="@+id/okbutton" 
                android:text="OK" 
                android:textColor="#ffffff"
                android:shadowColor="#000000"
                android:gravity="center" 
                android:layout_width="fill_parent"
                android:background="@drawable/buttonborderframe"/>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

try this. And you can hide and show the layout whenever you want. And other fields are accessable.




回答4:


Try adding this to your code:

window.setOutsideTouchable(true);

From the Android documentation:

void setOutsideTouchable (boolean touchable)

Controls whether the pop-up will be informed of touch events outside of its window. This only makes sense for pop-ups that are touchable but not focusable, which means touches outside of the window will be delivered to the window behind. The default is false.

If the popup is showing, calling this method will take effect only the next time the popup is shown or through a manual call to one of the update() methods.



来源:https://stackoverflow.com/questions/7271784/open-a-popupwindow-and-let-the-outsides-still-touchable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!