动态改变PopupWindow大小

动态改变PopupWindow大小

    最近项目需要实现动态改变PopupWindow的大小,今天下午花了两个小时没能搞定,现在在宿舍实现,简单将代码贴如下:

    PopupwindowActivity.java

[java]  view plain copy
  1. package com.test;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.res.Resources;  
  6. import android.graphics.drawable.ColorDrawable;  
  7. import android.graphics.drawable.Drawable;  
  8. import android.os.Bundle;  
  9. import android.view.Gravity;  
  10. import android.view.LayoutInflater;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.PopupWindow;  
  15. import android.widget.FrameLayout.LayoutParams;  
  16.   
  17. public class PopupwindowActivity extends Activity implements OnClickListener{  
  18.     /** Called when the activity is first created. */  
  19.   
  20.     private Button btn = null;  
  21.     private Button btn2 = null;  
  22.     private boolean click = false;  
  23.     PopupWindow mPopupWindow = null;      
  24.     View vPopunwind = null;  
  25.     Drawable mDrawable1 = null;  
  26.     Drawable mDrawable2 = null;  
  27.     Resources r = null;  
  28.     LayoutInflater mLayoutInflater = null;  
  29.       
  30.     @Override  
  31.     public void onCreate(Bundle savedInstanceState) {  
  32.         super.onCreate(savedInstanceState);  
  33.         setContentView(R.layout.main);  
  34.         btn = (Button)findViewById(R.id.btn);     
  35.         btn.setOnClickListener(this);    
  36.         r = this.getResources();  
  37.           
  38.         mDrawable1 = r.getDrawable(R.drawable.head1);  
  39.         mDrawable2 = r.getDrawable(R.drawable.head2);  
  40.           
  41.     }  
  42.     @Override  
  43.     public void onClick(View v) {  
  44.         Context mContext = PopupwindowActivity.this;     
  45.         if (v.getId() == R.id.btn) {  
  46.             mLayoutInflater = (LayoutInflater) mContext     
  47.             .getSystemService(LAYOUT_INFLATER_SERVICE);     
  48.             vPopunwind = mLayoutInflater.inflate(     
  49.                     R.layout.view, null,false);     
  50.               
  51. //          mPopupWindow = new PopupWindow(vPopunwind,LayoutParams.WRAP_CONTENT,     
  52. //                  LayoutParams.WRAP_CONTENT);     
  53.             mPopupWindow = new PopupWindow(mContext);  
  54.             mPopupWindow.setContentView(vPopunwind);  
  55.             mPopupWindow.setHeight(LayoutParams.WRAP_CONTENT);  
  56.             mPopupWindow.setWidth(LayoutParams.WRAP_CONTENT);  
  57.             mPopupWindow.setFocusable(true);  
  58.             mPopupWindow.setOutsideTouchable(true);  
  59.             mPopupWindow.setBackgroundDrawable(new ColorDrawable(0));  
  60.                       
  61.             mPopupWindow.showAtLocation(mLayoutInflater.inflate(     
  62.                     R.layout.main,null,false), Gravity.LEFT|Gravity.TOP, 120200);     
  63.           
  64.             btn2 = (Button) vPopunwind.findViewById(R.id.btn2);  
  65.             btn2.setOnClickListener(this);  
  66.         }  
  67.           
  68.         if(v.getId() == R.id.btn2) {  
  69.             if(click){  
  70.                 LayoutParams lp = (LayoutParams) vPopunwind.getLayoutParams();  
  71.                 lp.height=180;  
  72.                 lp.width=180;  
  73.                 vPopunwind.setLayoutParams(lp);  
  74.                 vPopunwind.setBackgroundDrawable(mDrawable1);  
  75.                 mPopupWindow.setContentView(vPopunwind);  
  76.                 mPopupWindow.setHeight(LayoutParams.WRAP_CONTENT);  
  77.                 mPopupWindow.setWidth(LayoutParams.WRAP_CONTENT);  
  78.                 mPopupWindow.setFocusable(true);  
  79.                 mPopupWindow.setOutsideTouchable(true);  
  80.                           
  81.                 mPopupWindow.showAtLocation(mLayoutInflater.inflate(     
  82.                         R.layout.main,null,false), Gravity.LEFT|Gravity.TOP, 120200);     
  83.             }else{  
  84.                 LayoutParams lp = (LayoutParams) vPopunwind.getLayoutParams();  
  85.                 lp.height=139;  
  86.                 lp.width=151;  
  87.                 vPopunwind.setLayoutParams(lp);  
  88.                 vPopunwind.setBackgroundDrawable(mDrawable2);  
  89.                 mPopupWindow.setContentView(vPopunwind);  
  90.                 mPopupWindow.setHeight(LayoutParams.WRAP_CONTENT);  
  91.                 mPopupWindow.setWidth(LayoutParams.WRAP_CONTENT);  
  92.                 mPopupWindow.setFocusable(true);  
  93.                 mPopupWindow.setOutsideTouchable(true);  
  94.           
  95.                           
  96.                 mPopupWindow.showAtLocation(mLayoutInflater.inflate(     
  97.                         R.layout.main,null,false), Gravity.LEFT|Gravity.TOP, 120200);  
  98.             }  
  99.               
  100.             click=!click;  
  101.         }  
  102.   
  103.     }  
  104. }  

注:有点比较奇怪,就是LayoutParams lp = (LayoutParams) vPopunwind.getLayoutParams();中的LayoutParams,只能是FrameLayout ,要不会报异常。



后记:

如果是其它布局类型,比如LinearLayout,则使用LinearLayout.LayoutParams。

LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) XXX.getLayoutParams();

不一定使用在PopupWindow上,可以动态改变任何布局或子布局。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值