当前位置:首页 > 芯闻号 > 充电吧
[导读]public class PopupWindow extends Objectjava.lang.Object    ↳ android.widget.PopupWindow Class Overv

public class PopupWindow extends Object
java.lang.Object    ↳ android.widget.PopupWindow Class Overview A popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity. PopupWindow的构造函数:

//方法一:  
public PopupWindow (Context context)  
//方法二:  
public PopupWindow(View contentView)  
//方法三:  
public PopupWindow(View contentView, int width, int height)  
//方法四:  
public PopupWindow(View contentView, int width, int height, boolean focusable)


注意:这里有四个构造函数,但要生成一个PopupWindow最基本的三个条件是一定要设置的:View contentView,int width, int height ;少任意一个就不可能弹出来PopupWindow!!
构造一个PopupWindow:

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.popwindowlayout, null);

		// 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()

		PopupWindow window = new PopupWindow(view,
				WindowManager.LayoutParams.MATCH_PARENT,
				WindowManager.LayoutParams.WRAP_CONTENT);

PopupWindow没有默认布局,所以需要设置width和height。 设置显示的位置的方法:

//相对某个控件的位置(正左下方),无偏移  
showAsDropDown(View anchor):  
//相对某个控件的位置,有偏移;xoff表示x轴的偏移,正值表示向左,负值表示向右;yoff表示相对y轴的偏移,正值是向下,负值是向上;  
showAsDropDown(View anchor, int xoff, int yoff):  
//相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移  
showAtLocation(View parent, int gravity, int x, int y):

设置其他属性的函数:

public void dismiss()  
public void setFocusable(boolean focusable)  //设置窗体可点击
public void setTouchable(boolean touchable)  
public void setOutsideTouchable(boolean touchable)  
public void setBackgroundDrawable(Drawable background)  //设置半透明,透明等背景

简单示例:(setAnimationStyle()、showAtLocation()) 布局文件activity_main.xml


PopupWindow弹出窗口的布局popwindowlayout.xml

	

窗口隐藏的动画:res/anim/pophidden_anim.xml


窗口显示的动画:res/anim/popshow_anim.xml

	

在res/values/styles.xml 中添加动画类型:

@anim/popshow_anim@anim/pophidden_anim" _ue_custom_node_="true">



点击按钮弹出驾校介绍窗口,点击屏幕别处,窗口消失。

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭