Android-popupWindow的简单使用

首先,popupWindowAlertDialog的 区 别 :

         1、AlertDialog不能指定显示位置,只能默认显示在屏幕最中间(当然也可以通过设置WindowManager参数来改变位置)

          2、PopupWindow是可以指定显示位置的,随便哪个位置都可以,更加灵活。

使用:

构造函数: 1、

//方法一
public PopupWindow (Context context) 
//方法二
public PopupWindow(View ContentView)
//方法三
public PopupWindow(View ContextView,int width,int height)
// ContextView是用于PopupWindow中的View,width是PopupWindow的宽度,    
// height是PopupWindow的高度
//方法四
public PopupWindow(View ContextView,int width,int height,boolean focusable)
// ContextView是用于PopupWindow中的View,width是PopupWindow的宽度,    
// height是PopupWindow的高度,focusable是指定PopupWindow能否获得焦点

             注意:要想显示一个popupWindow有三个最基本的条件 长,高以及在popupWindow中显示的view

                        也就是说:contentView,width,height 

             如果想用前两种构造:

        //方法一
        View contentView = 
        LayoutInflater.from(Demo21Activity.this).inflate(R.layout.xrlv_layout, null);
        PopupWindow popupWindow = new PopupWindow(MainActivity.this);
        popupWindow.setContentView(contentView);
        //设置宽为包裹内容
        popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置高为包裹内容
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //方法二
        View contentView = 
        LayoutInflater.from(MainActivity.this).inflate(R.layout.xrlv_layout, null);
        PopupWindow popupWindow = new PopupWindow(contentView);
        popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

             显示方法:

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

       PopWindow的使用:

  •        点击PopupWindow外部消失

               setOutsideTouchable(boolean touchable)

               这个函数的意义,就是指,PopupWindow以外的区域是否可点击,即如果点击 

               PopupWindow以外的区域,PopupWindow是否会消失。

  •                设置PopupWindow的背景  

              setBackgroundDrawable(Drawable background)

              能设置PopupWindow背景,只有添加上背景之后,setOutsideTouchable()才会生效

              PopupWindow设置完背景之后,PopupWindow才会对手机的返回按钮有响应:即,点击 

             手机返回按钮,可以关闭PopupWindow;如果不加setBackgroundDrawable()将关闭

             的PopupWindow所在的Activity

             这个函数要怎么用:可以填充进去各种Drawable,比如new BitmapDrawable(),new ColorDrawable()等

        PopupWindwo添加动画:

     PopupWindow可以进出场可以设置一些动画,使popupWindow的出现看得不那么突然,方法是:setAnimationStyle();

          滑入:

         

<!-- res/anim/pop_in.xml -->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0"
        android:toXDelta="0"
        android:fromYDelta="100%"
        android:toYDelta="0"
        android:duration="200" >
    </translate>
</set>

         滑出:

          

<!-- res/anim/pop_out.xml -->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0"
        android:toXDelta="0"
        android:fromYDelta="0"
        android:toYDelta="100%"
        android:duration="200" >
    </translate>
</set>

 设置一个滑动的动画样式:

1 <!-- res/values/styles.xml -->
2     <style name="popAnimStyle">
3         <item name="android:windowEnterAnimation">@anim/pop_in</item>
4         <item name="android:windowExitAnimation">@anim/pop_out</item>
5     </style>

设置动画:

 popupWindow.setAnimationStyle(R.style.popAnimStyle);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值