onSizeChange方法 - getWidth和GetHeight

时间:2012-10-16 11:56:01

标签: android android-layout

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

  • onsizechange方法总是返回yOld和yNew Zero。

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

1 个答案:

答案 0 :(得分:0)

在我的情况下,不需要MyCustomClass并覆盖onsizechanged方法来获得新的布局大小。因为小错误,我花了很多时间。我正在使用以下代码行限制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();

            }
        });

    }