2012-10-16 26 views
0

我想顯示可變大小的彈出窗口。該佈局由EditView組成,我在運行時設置文本,然後顯示彈出窗口。我想要新的佈局的高度。我正在使用以下代碼。任何人都可以幫助我解決我的問題和錯誤的方法。onSizeChange方法 - getWidth和GetHeight

  • onsizechange方法總是返回yOld和yNew零。

    私人無效信息(字符串名稱,字符串描述,視圖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); 
        } 
    } 
    

回答

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

      } 
     }); 

    } 
相關問題