动态改变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上,可以动态改变任何布局或子布局。