动态改变PopupWindow大小

动态改变PopupWindow大小

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

    PopupwindowActivity.java

package com.test;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.FrameLayout.LayoutParams;

public class PopupwindowActivity extends Activity implements OnClickListener{
	/** Called when the activity is first created. */

	private Button btn = null;
	private Button btn2 = null;
	private boolean click = false;
	PopupWindow mPopupWindow = null;	
	View vPopunwind = null;
	Drawable mDrawable1 = null;
	Drawable mDrawable2 = null;
	Resources r = null;
	LayoutInflater mLayoutInflater = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		btn = (Button)findViewById(R.id.btn);   
		btn.setOnClickListener(this);  
		r = this.getResources();
		
		mDrawable1 = r.getDrawable(R.drawable.head1);
		mDrawable2 = r.getDrawable(R.drawable.head2);
		
	}
	@Override
	public void onClick(View v) {
		Context mContext = PopupwindowActivity.this;   
		if (v.getId() == R.id.btn) {
			mLayoutInflater = (LayoutInflater) mContext   
			.getSystemService(LAYOUT_INFLATER_SERVICE);   
			vPopunwind = mLayoutInflater.inflate(   
					R.layout.view, null,false);   
			
//			mPopupWindow = new PopupWindow(vPopunwind,LayoutParams.WRAP_CONTENT,   
//					LayoutParams.WRAP_CONTENT);   
			mPopupWindow = new PopupWindow(mContext);
			mPopupWindow.setContentView(vPopunwind);
			mPopupWindow.setHeight(LayoutParams.WRAP_CONTENT);
			mPopupWindow.setWidth(LayoutParams.WRAP_CONTENT);
			mPopupWindow.setFocusable(true);
			mPopupWindow.setOutsideTouchable(true);
			mPopupWindow.setBackgroundDrawable(new ColorDrawable(0));
					
			mPopupWindow.showAtLocation(mLayoutInflater.inflate(   
					R.layout.main,null,false), Gravity.LEFT|Gravity.TOP, 120, 200);   
		
			btn2 = (Button) vPopunwind.findViewById(R.id.btn2);
			btn2.setOnClickListener(this);
		}
		
		if(v.getId() == R.id.btn2) {
			if(click){
				LayoutParams lp = (LayoutParams) vPopunwind.getLayoutParams();
				lp.height=180;
				lp.width=180;
				vPopunwind.setLayoutParams(lp);
				vPopunwind.setBackgroundDrawable(mDrawable1);
				mPopupWindow.setContentView(vPopunwind);
				mPopupWindow.setHeight(LayoutParams.WRAP_CONTENT);
				mPopupWindow.setWidth(LayoutParams.WRAP_CONTENT);
				mPopupWindow.setFocusable(true);
				mPopupWindow.setOutsideTouchable(true);
						
				mPopupWindow.showAtLocation(mLayoutInflater.inflate(   
						R.layout.main,null,false), Gravity.LEFT|Gravity.TOP, 120, 200);   
			}else{
				LayoutParams lp = (LayoutParams) vPopunwind.getLayoutParams();
				lp.height=139;
				lp.width=151;
				vPopunwind.setLayoutParams(lp);
				vPopunwind.setBackgroundDrawable(mDrawable2);
				mPopupWindow.setContentView(vPopunwind);
				mPopupWindow.setHeight(LayoutParams.WRAP_CONTENT);
				mPopupWindow.setWidth(LayoutParams.WRAP_CONTENT);
				mPopupWindow.setFocusable(true);
				mPopupWindow.setOutsideTouchable(true);
		
						
				mPopupWindow.showAtLocation(mLayoutInflater.inflate(   
						R.layout.main,null,false), Gravity.LEFT|Gravity.TOP, 120, 200);
			}
			
			click=!click;
		}

	}
}

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



后记:

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

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

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

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值