PopUpWindow使用详解(一)——基本使用

PopUpWindow使用详解(一)——基本使用,第1张

概述先看一下我们要做的效果:这个效果很容易理解:当点击btn时,在底部d出PopupWindow,然后点击各个itemd出对应toast。一、概述1、PopupWindow与AlertDialog的区别最关键的区别是AlertDialog不能指定显示位置,只能默认显示在屏幕最中间(当然也可以通过设置WindowManager参数来改变位置)。而PopupWindow是可以指定...

先看一下我们要做的效果:

这个效果很容易理解:当点击btn时,在底部d出PopupWindow,然后点击各个itemd出对应toast。

一、概述

 

1、PopupWindow与AlertDialog的区别

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

2、PopupWindow的相关函数 (1)、构造函数:
//方法一:public PopupWindow (Context context)//方法二:public PopupWindow(VIEw contentVIEw)//方法三:public PopupWindow(VIEw contentVIEw,int wIDth,int height)//方法四:public PopupWindow(VIEw contentVIEw,int height,boolean focusable)

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


所以,如果使用方法一来构造PopupWindow,那完整的构造代码应该是这样的:
 

VIEw contentVIEw = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout,null);PopupWinDWo popWnd = PopupWindow (context);popWnd.setContentVIEw(contentVIEw);popWnd.setWIDth(VIEwGroup.LayoutParams.WRAP_CONTENT);popWnd.setHeight(VIEwGroup.LayoutParams.WRAP_CONTENT);

有关为什么一定要设置wIDth和height的原因,我们后面会讲,这里说一下为什么样强制设置contentVIEw;很简单的原因是因为PopupWindow没有默认布局,它不会像AlertDialog那样只setTitle,就能d出来一个框。PopupWindow是没有默认布局的,它的布局只有通过我们自己设置才行。由于方法三中,含有了这三个必备条件,不用单独设置contentvIEw或者wIDth、height,所以构造方法三是用的最多的一个构造方法。
最后,方法四中的focusable变量不是必须的,有关它的方法和意义,我们会在下一篇中细讲。
 

(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):

这里有两种显示方式:
1、显示在某个指定控件的下方
showAsDropDown(VIEw anchor):
showAsDropDown(VIEw anchor,int yoff);
2、指定父视图,显示在父控件的某个位置(Gravity.top,Gravity.RIGHT等)
showAtLocation(VIEw parent,int y);
 

(3)、其它函数
public voID dismiss()//另外几个函数,这里不讲其意义,下篇细讲public voID setFocusable(boolean focusable)public voID settouchable(boolean touchable)public voID setoutsIDetouchable(boolean touchable)public voID setBackgroundDrawable(Drawable background)

这几个函数里,这篇只会用到dismiss(),用于不需要的时候,将窗体隐藏掉。

好了,废话不多说了,我们就做一个上面的例子来看一下。

二、简单示例(showAtLocation显示窗体)

在这个例子中,我们实现两个功能,d出popupWindow和Item点击响应

 

1、主布局(main.xml)

从效果图中也可以看到主布局只有一个button,什么都没有,所以它的布局代码哪下:

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"              androID:orIEntation="vertical"              androID:layout_wIDth="fill_parent"              androID:layout_height="fill_parent">    <button            androID:ID="@+ID/btn"            androID:layout_wIDth="fill_parent"            androID:layout_height="wrap_content"            androID:text="show popWindow"/></linearLayout>
2、PopupWindow布局(popuplayout.xml)

在概述中,我们提到了,必须为PopupWindow设置布局,从效果图中,我也可以看到它的布局有三个item,中间用横线分开。所以这里布局使用ListvIEw应该更合适,但为了减轻代码难度,我们直接使用TextVIEw和分隔线来代替,代码如下:

<?xml version="1.0" enCoding="utf-8"?> <linearLayout        xmlns:androID="http://schemas.androID.com/apk/res/androID"        androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content"        androID:background="#ffffff"        androID:orIEntation="vertical"        androID:paddingBottom="2dp">    <VIEw            androID:layout_wIDth="match_parent"            androID:layout_height="2.25dp"            androID:background="#fa7829"            androID:layout_alignParenttop="true"/>     <TextVIEw            androID:ID="@+ID/pop_computer"            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"                        androID:text="计算机"/>     <VIEw            androID:layout_wIDth="match_parent"            androID:layout_height="1dp"            androID:background="@drawable/List_line"/>     <TextVIEw            androID:ID="@+ID/pop_financial"            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"                        androID:text="金融"/>     <VIEw            androID:layout_wIDth="match_parent"            androID:layout_height="1dp"            androID:background="@drawable/List_line"/>     <TextVIEw            androID:ID="@+ID/pop_manage"            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"                        androID:text="管理"/>     <VIEw            androID:layout_wIDth="match_parent"            androID:layout_height="1dp"/> </linearLayout>

 

3、MainActivity代码

先贴出来完整代码,然后再逐步讲解:

public class MainActivity extends Activity implements VIEw.OnClickListener{     private PopupWindow mPopWindow;     @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.main);         button btn = (button) findVIEwByID(R.ID.btn);        btn.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                showPopupWindow();            }        });    }     private voID showPopupWindow() {	    //设置contentVIEw        VIEw contentVIEw = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout,null);        mPopWindow = new PopupWindow(contentVIEw,LayoutParams.WRAP_CONTENT,true);        mPopWindow.setContentVIEw(contentVIEw);		//设置各个控件的点击响应        TextVIEw tv1 = (TextVIEw)contentVIEw.findVIEwByID(R.ID.pop_computer);        TextVIEw tv2 = (TextVIEw)contentVIEw.findVIEwByID(R.ID.pop_financial);        TextVIEw tv3 = (TextVIEw)contentVIEw.findVIEwByID(R.ID.pop_manage);        tv1.setonClickListener(this);        tv2.setonClickListener(this);        tv3.setonClickListener(this);		//显示PopupWindow        VIEw rootvIEw = LayoutInflater.from(MainActivity.this).inflate(R.layout.main,null);        mPopWindow.showAtLocation(rootvIEw,Gravity.BottOM,0);     }     @OverrIDe    public voID onClick(VIEw v) {        int ID = v.getID();        switch (ID){            case R.ID.pop_computer:{                Toast.makeText(this,"clicked computer",Toast.LENGTH_SHORT).show();                mPopWindow.dismiss();            }            break;            case R.ID.pop_financial:{                Toast.makeText(this,"clicked financial",Toast.LENGTH_SHORT).show();                mPopWindow.dismiss();            }            break;            case R.ID.pop_manage:{                Toast.makeText(this,"clicked manage",Toast.LENGTH_SHORT).show();                mPopWindow.dismiss();            }            break;        }    }}
(1)首先看OnCreate()

在OnCreate()中只做了一个 *** 作,当点击button时,显示窗体:

public voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.main);     button btn = (button) findVIEwByID(R.ID.btn);    btn.setonClickListener(new VIEw.OnClickListener() {        @OverrIDe        public voID onClick(VIEw v) {            showPopupWindow();        }    });}
(2)、显示PopupWindow

下面是有关窗体 *** 作的代码:

private voID showPopupWindow() {	 //设置contentVIEw    VIEw contentVIEw = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout,null);    mPopWindow = new PopupWindow(contentVIEw,true); //设置各个控件的点击响应    TextVIEw tv1 = (TextVIEw)contentVIEw.findVIEwByID(R.ID.pop_computer);    TextVIEw tv2 = (TextVIEw)contentVIEw.findVIEwByID(R.ID.pop_financial);    TextVIEw tv3 = (TextVIEw)contentVIEw.findVIEwByID(R.ID.pop_manage);    tv1.setonClickListener(this);    tv2.setonClickListener(this);    tv3.setonClickListener(this);	//显示PopupWindow    VIEw rootvIEw = LayoutInflater.from(MainActivity.this).inflate(R.layout.main,null);    mPopWindow.showAtLocation(rootvIEw,0); }

这里同样分为三部分:
第一部分:设置ContentVIEw

VIEw contentVIEw = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout,null);mPopWindow = new PopupWindow(contentVIEw,true);

利用LayoutInflater获取R.layout.popuplayout对应的VIEw,然后利用我们上面所讲的构造函数三来生成mPopWindow;
这里 要注意一个问题:在这个构造函数里,我们传进去了wIDth和height全部都是WRAP_CONTENT;而在R.layout.popuplayou的根布局中,我们定义的wIDth和height代码是:layout_wIDth="fill_parent",layout_height="wrap_content";原代码如下:
 

<?xml version="1.0" enCoding="utf-8"?> <linearLayout        xmlns:androID="http://schemas.androID.com/apk/res/androID"        androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content"        androID:background="#ffffff"        androID:orIEntation="vertical"        androID:paddingBottom="2dp">                ………………</linearLayout>        

这里就有冲突了,那显示出来的popupWindow是按哪个来的呢?
从效果图中来看,明显PopupWindow宽度并没有全屏,显然是按代码中的布局为准。
这说明了:
**如果在代码中重新设置了popupWindow的宽和高,那就以代码中所设置为准。**(至于原因,下篇会讲)

第二部分:设置各个控件的点击响应

TextVIEw tv1 = (TextVIEw)contentVIEw.findVIEwByID(R.ID.pop_computer);TextVIEw tv2 = (TextVIEw)contentVIEw.findVIEwByID(R.ID.pop_financial);TextVIEw tv3 = (TextVIEw)contentVIEw.findVIEwByID(R.ID.pop_manage);tv1.setonClickListener(this);tv2.setonClickListener(this);tv3.setonClickListener(this);

这部分没什么好讲了,设置PopupWindow中各个控件的点击响应,但一定要注意的是,PopupWindow中各个控件的所在的布局是contentVIEw,而不是在Activity中,如果大家直接使用

TextVIEw tv1 = (TextVIEw)findVIEwByID(R.ID.pop_computer);

肯定会报错,因为R.ID.pop_computer这个ID值在当前Activtiy的布局文件中是找不到的。只有在R.layout.popuplayout的布局文件中才会有!所以,这就是为什么,要在findVIEwByID(R.ID.pop_computer)前指定contentVIEw的原因!!!在实际项目中,很容易遇到像这种需要指定根布局的情况,大家需要注意。
有关响应,就没什么好讲的了,因为我们在类顶部派生了VIEw.OnClickListener,所以在OnClick函数中,直接处理即可,代码如下:(在点击不同的Item时,一边d出不同的toast,一边将PopupWindow隐藏掉)
 

@OverrIDepublic voID onClick(VIEw v) {    int ID = v.getID();    switch (ID){        case R.ID.pop_computer:{            Toast.makeText(this,Toast.LENGTH_SHORT).show();            mPopWindow.dismiss();        }        break;        case R.ID.pop_financial:{            Toast.makeText(this,Toast.LENGTH_SHORT).show();            mPopWindow.dismiss();        }        break;        case R.ID.pop_manage:{            Toast.makeText(this,Toast.LENGTH_SHORT).show();            mPopWindow.dismiss();        }        break;    }}

第三部分:showAtLocation显示窗体

VIEw rootvIEw = LayoutInflater.from(MainActivity.this).inflate(R.layout.main,null);mPopWindow.showAtLocation(rootvIEw,0);

showAtLocation的显示就将PopupWindow的实例放在一个父容器中,然后指定显示在父容器中的位置。
由于,我们要将mPopWindow放在整个屏幕的最低部,所以我们将R.layout.main做为它的父容器。将其显示在BottOM的位置。

 

到这里,有关PopupWindow的显示及其中控件响应基本都讲完了,下面,我们就讲讲showAsDropDown显示窗体的用法。

 

 

三、另一示例(showAsDropDown显示窗体)

大家先看下面这个效果图:

这个效果图演示的是,在点击标题栏右方的“菜单”按钮时,在其下方显示一个自定义的菜单列表。

1、同样,我们先看看主布局代码(main.xml)
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"              androID:orIEntation="vertical"              androID:layout_wIDth="fill_parent"              androID:layout_height="fill_parent">     <relativeLayout androID:layout_wIDth="fill_parent"                    androID:layout_height="wrap_content"                    androID:background="#ffffff">         <TextVIEw androID:layout_wIDth="wrap_content"                  androID:layout_height="wrap_content"                  androID:layout_alignParentleft="true"                  androID:textcolor="#50484b"                  androID:padding="10dp"                  androID:text="返回"/>         <TextVIEw                androID:ID="@+ID/menu"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:layout_alignParentRight="true"                androID:textcolor="#50484b"                androID:padding="10dp"                androID:text="菜单"/>     </relativeLayout></linearLayout>

这段代码的布局很简单,就是生成一个标题栏,上面有两个按钮,“返回”和“菜单”

2、PopupWindow布局代码(popuplayout.xml)

这部分布局也不难,只得利用纯代码硬生成一个列表的布局。在实际项目中,大家应该使用ListvIEw来动态生成列表,这样生成的popupWindow就是可以复用的了。有关布局就不再多讲,跟上面的布局基本一样,只是换了背景。

<?xml version="1.0" enCoding="utf-8"?><linearLayout        xmlns:androID="http://schemas.androID.com/apk/res/androID"        androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content"        androID:background="@drawable/pop_bg"        androID:orIEntation="vertical"        androID:paddingBottom="2dp">     <TextVIEw            androID:ID="@+ID/pop_computer"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"                        androID:text="计算机"/>     <VIEw            androID:layout_wIDth="match_parent"            androID:layout_height="1dp"            androID:background="@drawable/List_line"/>     <TextVIEw            androID:ID="@+ID/pop_financial"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"                        androID:text="金融"/>     <VIEw            androID:layout_wIDth="match_parent"            androID:layout_height="1dp"            androID:background="@drawable/List_line"/>     <TextVIEw            androID:ID="@+ID/pop_manage"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"                        androID:text="管理"/>     <VIEw            androID:layout_wIDth="match_parent"            androID:layout_height="1dp"/> </linearLayout>
3、MainActivity代码

同样是先贴出来完整代码,然后再细讲。

public class MainActivity extends Activity implements VIEw.OnClickListener{    private PopupWindow mPopWindow;    private TextVIEw mMenuTv;    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.main);        mMenuTv = (TextVIEw)findVIEwByID(R.ID.menu);        mMenuTv.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                showPopupWindow();            }        });    }    private voID showPopupWindow() {        VIEw contentVIEw = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout,null);        mPopWindow = new PopupWindow(contentVIEw);        mPopWindow.setWIDth(VIEwGroup.LayoutParams.WRAP_CONTENT);        mPopWindow.setHeight(VIEwGroup.LayoutParams.WRAP_CONTENT);         TextVIEw tv1 = (TextVIEw)contentVIEw.findVIEwByID(R.ID.pop_computer);        TextVIEw tv2 = (TextVIEw)contentVIEw.findVIEwByID(R.ID.pop_financial);        TextVIEw tv3 = (TextVIEw)contentVIEw.findVIEwByID(R.ID.pop_manage);        tv1.setonClickListener(this);        tv2.setonClickListener(this);        tv3.setonClickListener(this);         mPopWindow.showAsDropDown(mMenuTv);    }     @OverrIDe    public voID onClick(VIEw v) {        int ID = v.getID();        switch (ID){            case R.ID.pop_computer:{                Toast.makeText(this,Toast.LENGTH_SHORT).show();                mPopWindow.dismiss();            }            break;        }    }}

这段代码的意义就是点击menud出popupwindow,然后对各个item进行响应,我们主要讲讲showPopupWindow() 这部分,对于item响应的部分与上个示例都一样,就不再细讲。

private voID showPopupWindow() {    VIEw contentVIEw = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout,null);    mPopWindow = new PopupWindow(contentVIEw);    mPopWindow.setWIDth(VIEwGroup.LayoutParams.WRAP_CONTENT);    mPopWindow.setHeight(VIEwGroup.LayoutParams.WRAP_CONTENT);     TextVIEw tv1 = (TextVIEw)contentVIEw.findVIEwByID(R.ID.pop_computer);    TextVIEw tv2 = (TextVIEw)contentVIEw.findVIEwByID(R.ID.pop_financial);    TextVIEw tv3 = (TextVIEw)contentVIEw.findVIEwByID(R.ID.pop_manage);    tv1.setonClickListener(this);    tv2.setonClickListener(this);    tv3.setonClickListener(this);     mPopWindow.showAsDropDown(mMenuTv);}

这里首先注意的一个地方,在这个示例中,我们是使用的构造方法二来生成的PopupWindow实例的,同样,再强调一遍:contentVIEw,WIDth,Height这三个元素是必须设置的,缺一不可!至于为什么要显式设置WIDth,Height,我们下篇会讲到。

VIEw contentVIEw = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout,null);mPopWindow = new PopupWindow(contentVIEw);mPopWindow.setWIDth(VIEwGroup.LayoutParams.WRAP_CONTENT);mPopWindow.setHeight(VIEwGroup.LayoutParams.WRAP_CONTENT);

然后就是使用showAsDropDown()显示PopupWindow:

mPopWindow.showAsDropDown(mMenuTv);

大家也现样可以使用showAsDropDown(VIEw anchor,int yoff):来添加相对x轴和y轴的位移量。具体用法就不再细讲,没什么难度,大家试一试即可。
好了,这部分示例也讲完了,下面我们就在这个示例上升级一个功能:讲讲怎么在d出PopupWindow的同时利用阴影把背景全部给遮罩起来。
 

四、提高:为菜单添加阴影

这部分的效果图是下面这样的:

从效果图中可以看出,这部分是上一个示例的升级版,就是在点出PopupWindow的同时,把背景用半透明黑色遮罩住,像d出AlertDialog一样的效果。下面就来看看这个效果是怎么实现的吧。

 

1、PopupWindow布局(popuplayout.xml)

其实原理很简单,使PopupWindow的界面充满全屏,而实际的列表菜单只是其中的一个子布局即可,所以此时的PopupWindow的布局代码如下:

<?xml version="1.0" enCoding="utf-8"?><relativeLayout        xmlns:androID="http://schemas.androID.com/apk/res/androID"        androID:layout_wIDth="fill_parent"        androID:layout_height="fill_parent"        androID:background="#66000000">    <linearLayout            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:background="@drawable/pop_bg"            androID:orIEntation="vertical"            androID:paddingBottom="2dp"            androID:layout_alignParentRight="true">         <TextVIEw                androID:ID="@+ID/pop_computer"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                                androID:text="计算机"/>         <VIEw                androID:layout_wIDth="match_parent"                androID:layout_height="1dp"                androID:background="@drawable/List_line"/>         <TextVIEw                androID:ID="@+ID/pop_financial"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                                androID:text="金融"/>         <VIEw                androID:layout_wIDth="match_parent"                androID:layout_height="1dp"                androID:background="@drawable/List_line"/>         <TextVIEw                androID:ID="@+ID/pop_manage"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                                androID:text="管理"/>         <VIEw                androID:layout_wIDth="match_parent"                androID:layout_height="1dp"/>     </linearLayout></relativeLayout>

可见在列表项外面又包了一层relativeLayout,将列表的根布局linearLayout靠父窗口右边显示即可。给relativeLayout添加了半透明背景androID:background="#66000000"
这样要非常注意的是,根布局relativeLayout设置的androID:layout_wIDth和androID:layout_height是无意义的,因为我们会通过代码重新设置:
 

private voID showPopupWindow() {    VIEw contentVIEw = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout,null);    mPopWindow = new PopupWindow(contentVIEw);    mPopWindow.setWIDth(VIEwGroup.LayoutParams.FILL_PARENT);    mPopWindow.setHeight(VIEwGroup.LayoutParams.FILL_PARENT);      ………………//设置各控件点击响应     mPopWindow.showAsDropDown(mMenuTv); }

在这里,我们通过代码将PopupWindow的wIDth和height设置为LayoutParams.FILL_PARENT。那大家可能会有疑问:为什么我在xml中布局中明明写好了根布局relativeLayout的宽和高,为什么非要我们在代码中重新设置呢?不设置还显示不出来…………
下篇,我们将会解答这个问题。
好啦,这个示例关键部分讲完了,其它的大家就看源码吧。
 

五、为PopupWindow添加动画

先看看效果:

为PopupWindow添加动画并不难,只需要使用一个函数即可:

//设置动画所对应的stylemPopWindow.setAnimationStyle(R.style.contextMenuAnim);

下面就来看看具体是如何添加动画的

1、生成动画对应的style (1)、进入时的动画:(context_menu_enter.xml)
<?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>

这段代码的意义就是从底部进入,即从Y轴100%的位置移动到0的位置。

(2)、退出时动画(context_menu_exit.xml)
<?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>

效果是从上向下移动。

(3)、最后,生成对应的style---contextMenuAnim
<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>

androID:windowEnteranimation设置进场动画,androID:windowExitAnimation设置出场动画。

 

2、使用AnimationStyle

使用时非常简单,直接将对应的style通过setAnimationStyle设置进PopupWindow实例即可,代码如下,难度不大,不再细讲。

private voID showPopupWindow() {    VIEw contentVIEw = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout,null);    mPopWindow = new PopupWindow(contentVIEw);    mPopWindow.setWIDth(VIEwGroup.LayoutParams.FILL_PARENT);    mPopWindow.setHeight(VIEwGroup.LayoutParams.FILL_PARENT);        ………………//设置各子项点击响应    mPopWindow.setAnimationStyle(R.style.contextMenuAnim);     mPopWindow.showAsDropDown(mMenuTv);}

到这里,这个示例的代码就讲完了。这篇讲述了有关PopupWindow的基本使用方法,下篇将针对还未讲述的几个函数,以及问题点结合源码深入剖析。

总结

以上是内存溢出为你收集整理的PopUpWindow使用详解(一)——基本使用全部内容,希望文章能够帮你解决PopUpWindow使用详解(一)——基本使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1089432.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存