Android-弹出窗口未显示在片段中 [英] Android - Popup window not showing in fragment

查看:81
本文介绍了Android-弹出窗口未显示在片段中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 popupWindow ,但是当我调用它时,它没有显示在我的 Fragment 上.这是我的popupWindow代码:

I'm creating a popupWindow but it is not showing on my Fragment when I call it.
Here is my code for the popupWindow:

LayoutInflater layoutInflater = 
            (LayoutInflater)getActivity()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View popupView = layoutInflater.inflate(R.layout.popup, null);
    final PopupWindow popupWindow = new PopupWindow(
            popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);


    Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
    btnDismiss.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View v) {
            popupWindow.dismiss();
        }});

    popupWindow.showAsDropDown(getView());
    //popupWindow.showAsDropDown(btnOpenPopup, 50, -30);
    //popupWindow.showAsDropDown(getCurrentFocus());
    popupView.setOnTouchListener(new OnTouchListener() {
        int orgX, orgY;
        int offsetX, offsetY;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                orgX = (int) event.getX();
                orgY = (int) event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                offsetX = (int)event.getRawX() - orgX;
                offsetY = (int)event.getRawY() - orgY;
                popupWindow.update(offsetX, offsetY, -1, -1, true);
                break;
            }
            return true;
        }});

推荐答案

以下代码可能适用于您的规范.从分配给View的OnClickListener的onClick(View v)内部调用此方法:

The following code may be work your specification. Call this method from inside onClick(View v) of OnClickListener assigned to the View:

public void showPopup(View anchorView) {

    View popupView = getLayoutInflater().inflate(R.layout.popup_layout, null);

    PopupWindow popupWindow = new PopupWindow(popupView, 
                           LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    // Example: If you have a TextView inside `popup_layout.xml`    
    TextView tv = (TextView) popupView.findViewById(R.id.tv);

    tv.setText(....);

    // Initialize more widgets from `popup_layout.xml`
    ....
    ....

    // If the PopupWindow should be focusable
    popupWindow.setFocusable(true);

    // If you need the PopupWindow to dismiss when when touched outside 
    popupWindow.setBackgroundDrawable(new ColorDrawable());

    int location[] = new int[2];

    // Get the View's(the one that was clicked in the Fragment) location
    anchorView.getLocationOnScreen(location);

    // Using location, the PopupWindow will be displayed right under anchorView
    popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, 
                                     location[0], location[1] + anchorView.getHeight());

}

这里的anchorView是onClick(View v)的v.

here anchorView is the v from onClick(View v).

这篇关于Android-弹出窗口未显示在片段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆