java popupwindow_PopupWindow的简单使用

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">

5ba97ed1d3ae708d34d63f07bb7a7fed.png

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值