【问题标题】:Android - Popup window not showing in fragmentAndroid - 片段中未显示弹出窗口
【发布时间】:2015-01-28 11:29:18
【问题描述】:

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

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;
        }});

【问题讨论】:

  • 在 popupwindow 对象上调用 show 方法。
  • 好吧,它在公共 void popups() 中,我称片段上的弹出窗口仍然没有出现,先生

标签: android android-fragments android-popupwindow


【解决方案1】:

以下代码可能符合您的规范。从分配给视图的 OnClickListener 的 onClick(View v) 内部调用此方法:

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。

【讨论】:

    【解决方案2】:

    使用以下代码显示弹出窗口。它会为你工作。

    View popupView = layoutInflater.inflate(R.layout.popup, null);
        final PopupWindow popupWindow = new PopupWindow(
                popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    popupWindow.showAtLocation(popupView , Gravity.CENTER, 0, 0);
    

    【讨论】:

      【解决方案3】:

      这里是显示和隐藏弹出窗口的示例代码。

          TextView popupWindowTextView = new TextView(getActivity()); 
          Button popupWindowButton = new Button(getActivity()); 
          LinearLayout layout = new LinearLayout(getActivity()); 
          popupWindowButton.setText("OK"); 
          popupWindowTextView.setText("Popup Window. Click on OK to dismiss."); 
          popupWindowTextView.setPadding(0, 0, 0, 20); 
          layout.setOrientation(1); 
          layout.addView(popupWindowTextView); 
          layout.addView(popupWindowButton);
      
          int popupWindowWidth = 200;
          int popupWindowHeight = 150;
          final PopupWindow popupWindow = new PopupWindow(context);
          popupWindow.setContentView(layout);
          popupWindow.setWidth(popupWidth);
          popupWindow.setHeight(popupHeight);
          popupWindow.setFocusable(true);
          popupWindow.showAtLocation(layout, Gravity.NO_GRAVITY,    popupWindowWidth, popupWindowHeight);
          popupWindowButton.setOnClickListener(new OnClickListener() {
      
               @Override
               public void onClick(View v) {
                 popupWindow.dismiss();
               }
          });
      

      【讨论】:

        【解决方案4】:
        final PopupWindow popupWindow = new PopupWindow(
                popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        

        检查您使用的 LayoutParams 是否与视图的父级匹配。 例如如果父级是LinearLayout,则使用LinearLayout.LayoutParams.WRAP_CONTENT

        【讨论】:

        • 这不是一回事。其他 LayoutParams 都继承自 ViewGroup.LayoutParamsWRAP_CONTENTMATCH_PARENT;他们没有自己的价值观。
        【解决方案5】:

        R.layout.popup 的根应该有

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        

        【讨论】:

          【解决方案6】:

          只需在showAsDropDown 之后立即使用update 即可:

          pop.showAsDropDown(anchor);
          pop.update(0,0, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-12-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-08-29
            • 2015-08-28
            相关资源
            最近更新 更多