onSizeChange method - getWidth and GetHeight



我想显示大小可变的弹出窗口。布局由 EditView 组成,我在显示弹出窗口之前在运行时设置文本。我想要新布局的高度。我正在使用以下代码。任何人都可以帮助我错的地方和错误的方法来解决我的问题。

  • onsizechange 方法始终返回 yOld 和 yNew Zero。

    私有无效信息(字符串名称、字符串描述、视图 v) {

        LayoutInflater layoutInflater = (LayoutInflater) getActivity()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View popupView = layoutInflater.inflate(R.layout.lo_popup_concerninfo,
                null, false);
    
        final LinearLayout ll = (LinearLayout) popupView.findViewById(R.id.lo_popup_concern_linearlayout);
        TextView tv_concernName = (TextView) popupView
                .findViewById(R.id.tv_concernName);
        final TextView tv_concernDesc = (TextView) popupView
                .findViewById(R.id.tv_concernDesc);
        Button btn_concernDone = (Button) popupView
                .findViewById(R.id.btn_concernDone);        
        tv_concernName.setText(name);
        tv_concernDesc.setText(Description);
        ll.addView(new MyCustomView(getActivity().getApplicationContext()));
        Display display = getActivity().getWindowManager().getDefaultDisplay();
            //popupView.measure(display.getWidth(), display.getHeight());
       popupView.measure(View.MeasureSpec.UNSPECIFIED,   View.MeasureSpec.UNSPECIFIED); 
          System.out.println("view.getMeasuredWidth() " +
                  popupView.getMeasuredWidth());
                  System.out.println("view.getMeasuredHeight() " +
                  popupView.getMeasuredHeight());
    
        final PopupWindow popup = new PopupWindow(popupView,
                display.getWidth() - 40, popupView.getMeasuredHeight() + finalHeight);
        System.out.println("view" +
                  popup.getHeight());
        popup.showAtLocation(v, Gravity.CENTER, 0, 10);
        popup.setFocusable(true);
        popup.update();
         class MyCustomView extends View{
         int viewWidth = 0;
         int viewHeight = 0;
         public MyCustomView(Context context) {
                 super(context);
                 System.out.println("ali1");
         }
         @Override
         protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld){
                 super.onSizeChanged(xNew, yNew, xOld, yOld);
                 viewWidth = xNew;
                 viewHeight = yNew;
                 System.out.println("xNew " + xNew);
                 System.out.println("yNew " + yNew);
                 System.out.println("xOld " + xOld);
                 System.out.println("yOld " + yOld);
         }
    
         @Override
         protected void onDraw(Canvas canvas){
                 super.onDraw(canvas);
                 String msg = "width: " + viewWidth + "height: " + viewHeight;
                 System.out.println(msg);
         }
    }
    

在我的情况下,不需要MyCustomClass和覆盖onsizechange方法来获得新的布局大小。因为一个小错误,我花了很多时间。我使用以下代码行来限制 TextView 拉伸和包装内容。

 final PopupWindow popup = new PopupWindow(popupView,
            display.getWidth() - 40, popupView.getMeasuredHeight() + finalHeight) 

现在我使用以下行

final PopupWindow popup = new PopupWindow(popupView,
                display.getWidth() - 40, ViewGroup.LayoutParams.WRAP_CONTENT);

完整方法如下。

private void info(String name, String Description, View v) {
        LayoutInflater layoutInflater = (LayoutInflater) getActivity()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View popupView = layoutInflater.inflate(R.layout.lo_popup_concerninfo,
                null, false);
        LinearLayout ll = (LinearLayout) popupView
                .findViewById(R.id.lo_popup_concern_linearlayout);
        TextView tv_concernName = (TextView) popupView
                .findViewById(R.id.tv_concernName);
        final TextView tv_concernDesc = (TextView) popupView
                .findViewById(R.id.tv_concernDesc);
        Button btn_concernDone = (Button) popupView
                .findViewById(R.id.btn_concernDone);
        tv_concernName.setText(name);
        tv_concernDesc.setText(Description);
        Display display = getActivity().getWindowManager().getDefaultDisplay();
        popupView.measure(display.getWidth(), display.getHeight());
        final PopupWindow popup = new PopupWindow(popupView,
                display.getWidth() - 40, ViewGroup.LayoutParams.WRAP_CONTENT);
        popup.showAtLocation(v, Gravity.CENTER, 0, 10);
        popup.setFocusable(true);
        popup.update();
        btn_concernDone.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popup.dismiss();
            }
        });
    }

最新更新