PopupWindow 使用

一.概述

1.PopupWindow与AlertDialog的区别
最关键的区别是AlertDialog不能指定显示位置,只能默认显示在屏幕最中间(当然也可以通过设置WindowManager参数来改变位置)。而PopupWindow是可以指定显示位置的,随便哪个位置都可以,更加灵活。
2.相关函数
(1)构造函数

//方法一:  
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!!!!
(2)显示函数

//相对某个控件的位置(正左下方),无偏移  
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):  

二.实例

下面给出一个例子,先看效果图:

这里写图片描述

点击右上角的菜单按钮,弹出一个PopupWindow,并且带有缩放动画

public class MainActivity extends AppCompatActivity {

    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textview);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showPopupWindow();
            }
        });
    }
    private void showPopupWindow() {
        View view = View.inflate(this,R.layout.item_layout,null);
        PopupWindow pw = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        //为PopupWindow添加动画
        pw.setAnimationStyle(R.style.popstyle);
        //经过我的实验发现,只有同时设置这个属性才能点击外部使PopupWindow消失
        pw.setOutsideTouchable(true);
        pw.setBackgroundDrawable(new ColorDrawable());
        pw.showAsDropDown(textView);
    }
}

下面看看怎么给PopupWindow 设置显示和消失动画
首先在styles文件中定义样式,指定两个属性名称,分别是窗体进入和消失动画

 <style name="popstyle"  >
        <item name="android:windowEnterAnimation">@anim/enter</item>
        <item name="android:windowExitAnimation">@anim/exit</item>
    </style>

然后定义我们自己的动画,这里使用缩放动画,代码如下:

进入动画

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
       android:fromXScale="0.0"
    android:toXScale="1.0"
    android:fromYScale="0.0"
    android:toYScale="1.0"
    android:pivotX="50%"
    android:pivotY="0"
        android:duration ="500">
</scale>

消失动画

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromYScale="1.0"
    android:fromXScale="1.0"
    android:toXScale="0"
    android:toYScale="0"
    android:pivotX="50%"
    android:pivotY="0"
    android:duration ="500"
        >
</scale>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值