Popwindow学习笔记

工作中已经使用很多次popwindow了。但是一直没有好好总结,今天乘此机会把popwindow过一遍。在activity中我们可以选择使用popwindow和dialog弹出某一个小界面,但是默认情况下dialog是居中显示的,popwindow支持任何位置,先比较而言更新灵活。大致分一下几个方面吧。

 一、popwindow的基本方法

 二、popwindow使用实例

 三、popwindow动画

 四、popwindow几个方法解析


一、popwindow的基本方法

首先我们来看看构造函数
 public PopupWindow() 
 public PopupWindow(View contentView)
 public PopupWindow(int width, int height)
 public PopupWindow(View contentView, int width, int height)
 public PopupWindow(View contentView, int width, int height, boolean focusable)

平时我们使用最多的就是第三个构造函数。请注意,在使用popwindow时,有三要素,缺一不可,contextView、width、height。

再看看显示的方法。

pop.showAsDropDown(anchor);
pop.showAsDropDown(anchor, xoff, yoff);
pop.showAsDropDown(anchor, xoff, yoff, gravity);
pop.showAtLocation(parent, gravity, x, y);

显示在某一个控件下方。或者是基于某一个控件显示。其中偏移量要注意一下,x大于0,是向左边,x小于0,是向右边。y大于0是向上边,y小于0是向下边。


二、popwindow使用实例

1. 比如在屏幕的底部显示出来。核心代码我贴出。

View contentView = LayoutInflater.from(this).inflate(R.layout.pop, null); 
PopupWindow pop = new PopupWindow();
pop.setContentView(contentView);
pop.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
pop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
pop.setBackgroundDrawable(new BitmapDrawable());
pop.setOutsideTouchable(true);
		
View rootView = LayoutInflater.from(this).inflate(R.layout.main, null);
pop.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);

我们来看看popwindow对应的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <View
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:background="#ff0000" />

    <TextView
        android:gravity="center"
        android:id="@+id/tv_study"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="学习" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#333333" />

    <TextView
         android:gravity="center"
        android:id="@+id/tv_life"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="生活" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#333333" />

    <TextView
         android:gravity="center"
        android:id="@+id/tv_work"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="工作" />

</LinearLayout>

根布局LinearLayout是指定大小了。为啥在创建popwindow时还需要指定宽和高呢。后边说明。

2. 模仿diglog的效果,弹出popwindow时,背景是有阴影效果的。

View contentView = LayoutInflater.from(this).inflate(R.layout.pop2, null); 
PopupWindow pop = new PopupWindow();
pop.setContentView(contentView);
pop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
pop.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
pop.setOutsideTouchable(true);
pop.showAsDropDown(btnShow);


详细代码可以下载。


三、popwindow动画

我们需要在style文件定义好动画
 <style name="contextMenuAnim" parent="@android:style/Animation.Activity">
    	<item name="android:windowEnterAnimation">@anim/context_menu_enter</item>
    	<item name="android:windowExitAnimation">@anim/context_menu_exit</item>
	</style>

context_menu_enter文件内容:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:duration="@android:integer/config_shortAnimTime"
        android:fromXDelta="0"
        android:fromYDelta="100%p"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toXDelta="0"
        android:toYDelta="0"/>

</set>
context_menu_exit文件内容:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="@android:integer/config_shortAnimTime"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toXDelta="0"
        android:toYDelta="100%p" />

</set>

通过代码pop.setAnimationStyle(R.style.contextMenuAnim);设置动画。


四、popwindow几个方法解析

<span style="font-family:Microsoft YaHei;">public void setTouchable(boolean touchable)
public void setFocusable(boolean focusable)
public void setOutsideTouchable(boolean touchable)
public void setBackgroundDrawable(Drawable background)</span>

1. setTouchable
popwindow是否响应touch事件。设置为ture的话,表示响应。为fouse的话,表示不响应,那么这时候在contentView上边的view的单击事件都没有了。

2. setFocusable
popwindow是否具有获取焦点的能力。默认是为false。一般情况下我们也用不到。只有在popwindow的contentView中具有Edittext时,是需要焦点的话,如果没有焦点的话,就可能没法正常输入。

3. setOutsideTouchable
这个方法的意思就是popwindow以外的区域是否可以点击。设置为true以后,点击popwindow以外的区域,popwindow就会消失。但是要注意单独设置个属性是没有作用的。必须要同时设置两个属性才行。
pop.setBackgroundDrawable(new BitmapDrawable());
pop.setOutsideTouchable(true);

4. setBackgroundDrawable
设置这个方法以后,按下返回键,pop也能消失了。点击popwindow中的某一行也是能够消失的。具体原理可参考;http://blog.csdn.net/harvic880925/article/details/49278705









  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
弹出可移动的层,有多种弹出方式 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <script language='javascript' src='pop.js'></script> <TITLE> JS弹出浮动层窗口_演示_www.codefans.net </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <BODY> <Script Language="JavaScript"> function ShowIframe() //显示iframe { var pop=new Popup({ contentType:1,isReloadOnClose:false,width:400,height:500}); pop.setContent("contentUrl","http://www.codefans.net"); pop.setContent("title","框架iframe示例"); pop.build(); pop.show(); } function ShowHtmlString() //显示html { var strHtml = "<table border=1 style='width:90%; text-align:center;'><tr style='height:40px'><td>ds</td><td>dads</td></tr><tr style='height:40px'><td>dadas</td><td>dasd</td></tr><tr style='height:40px'><td>dadasd</td><td>dsadads</td></tr></table>"; var pop=new Popup({ contentType:2,isReloadOnClose:false,width:340,height:300}); pop.setContent("contentHtml",strHtml); pop.setContent("title","字符串示例html"); pop.build(); pop.show(); } function ShowAlert() //显示警示对话框 { var pop=new Popup({ contentType:4,isReloadOnClose:true,width:340,height:80}); pop.setContent("title","源码爱好者警告框示例"); pop.setContent("alertCon","您好!欢迎来到源码爱好者!"); pop.build(); pop.show(); } function ShowConfirm() //显示确认对话框 { var pop=new Popup({ contentType:3,isReloadOnClose:false,width:340,height:80}); pop.setContent("title","对话框示例confirm"); pop.setContent("confirmCon","您好!您要关闭源码爱好者吗??"); pop.setContent("callBack",ShowCallBack); pop.setContent("parameter",{id:"divCall",str:"点击确定后显示的字符串",obj:pop}); pop.build(); pop.show(); } function ShowCallBack(para) //回调函数 { var o_pop = para["obj"] var obj = document.getElementById(para["id"]); o_pop.close(); obj.innerText = para["str"]; } </Script> <INPUT TYPE="button" value="ShowHtmlString()" onclick="ShowHtmlString();"> <br> <INPUT TYPE="button" value="ShowIframe()" onclick="ShowIframe();"> <br> <INPUT TYPE="button" value="ShowAlert()" onclick="ShowAlert();"> <br> <INPUT TYPE="button" value="ShowConfirm()" onclick="ShowConfirm();"> </BODY> </HTML>

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值