Android PopupWindow使用方法小結(jié)
前幾天要用到PopupWindow,一時(shí)竟想不起來怎么用,趕緊上網(wǎng)查了查,自己寫了個(gè)demo,并在此記錄一下PopupWindow的用法。
使用場(chǎng)景
PopupWindow,顧名思義,就是彈窗,在很多場(chǎng)景下都可以見到它。例如ActionBar/Toolbar的選項(xiàng)彈窗,一組選項(xiàng)的容器,或者列表等集合的窗口等等。
基本用法
使用PopupWindow很簡單,可以總結(jié)為三個(gè)步驟:
- 創(chuàng)建PopupWindow對(duì)象實(shí)例;
- 設(shè)置背景、注冊(cè)事件監(jiān)聽器和添加動(dòng)畫;
- 顯示PopupWindow。
其中,第二步是可選的(不過基本上都要進(jìn)行第二步的設(shè)置)。下面是一個(gè)簡單的例子:
// 用于PopupWindow的View View contentView=LayoutInflater.from(context).inflate(layoutRes, null, false); // 創(chuàng)建PopupWindow對(duì)象,其中: // 第一個(gè)參數(shù)是用于PopupWindow中的View,第二個(gè)參數(shù)是PopupWindow的寬度, // 第三個(gè)參數(shù)是PopupWindow的高度,第四個(gè)參數(shù)指定PopupWindow能否獲得焦點(diǎn) PopupWindow window=new PopupWindow(contentView, 100, 100, true); // 設(shè)置PopupWindow的背景 window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 設(shè)置PopupWindow是否能響應(yīng)外部點(diǎn)擊事件 window.setOutsideTouchable(true); // 設(shè)置PopupWindow是否能響應(yīng)點(diǎn)擊事件 window.setTouchable(true); // 顯示PopupWindow,其中: // 第一個(gè)參數(shù)是PopupWindow的錨點(diǎn),第二和第三個(gè)參數(shù)分別是PopupWindow相對(duì)錨點(diǎn)的x、y偏移 window.showAsDropDown(anchor, xoff, yoff); // 或者也可以調(diào)用此方法顯示PopupWindow,其中: // 第一個(gè)參數(shù)是PopupWindow的父View,第二個(gè)參數(shù)是PopupWindow相對(duì)父View的位置, // 第三和第四個(gè)參數(shù)分別是PopupWindow相對(duì)父View的x、y偏移 // window.showAtLocation(parent, gravity, x, y);
每個(gè)方法的作用都寫在注解里了,相信大家都能看懂。不過這里要注意這兩行:
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); window.setOutsideTouchable(true);
只有同時(shí)設(shè)置PopupWindow的背景和可以響應(yīng)外部點(diǎn)擊事件,它才能“真正”響應(yīng)外部點(diǎn)擊事件。也就是說,當(dāng)你點(diǎn)擊PopupWindow的外部或者按下“Back”鍵時(shí),PopupWindow才會(huì)消失。
使用showAsDropDown方法顯示PopupWindow
通常情況下,調(diào)用showAsDropDown方法后PopupWindow將會(huì)在錨點(diǎn)的左下方顯示(drop down)。但是,有時(shí)想讓PopupWindow在錨點(diǎn)的上方顯示,或者在錨點(diǎn)的中間位置顯示,此時(shí)就需要用到showAsDropDown方法的xoff和yoff參數(shù)了。
這里我們的目的不僅包括上面提到的兩種情況(錨點(diǎn)上方或錨點(diǎn)中部),而是囊括了水平和垂直方向各5種顯示方式:
水平方向:
ALIGN_LEFT:在錨點(diǎn)內(nèi)部的左邊;
ALIGN_RIGHT:在錨點(diǎn)內(nèi)部的右邊;
CENTER_HORI:在錨點(diǎn)水平中部;
TO_RIGHT:在錨點(diǎn)外部的右邊;
TO_LEFT:在錨點(diǎn)外部的左邊。
垂直方向:
ALIGN_ABOVE:在錨點(diǎn)內(nèi)部的上方;
ALIGN_BOTTOM:在錨點(diǎn)內(nèi)部的下方;
CENTER_VERT:在錨點(diǎn)垂直中部;
TO_BOTTOM:在錨點(diǎn)外部的下方;
TO_ABOVE:在錨點(diǎn)外部的上方。
下面來看張圖:


我們先定義一個(gè)類對(duì)PopupWindow進(jìn)行簡單的封裝:
public abstract class CommonPopupWindow {
protected Context context;
protected View contentView;
protected PopupWindow mInstance;
public CommonPopupWindow(Context c, int layoutRes, int w, int h) {
context=c;
contentView=LayoutInflater.from(c).inflate(layoutRes, null, false);
initView();
initEvent();
mInstance=new PopupWindow(contentView, w, h, true);
initWindow();
}
public View getContentView() { return contentView; }
public PopupWindow getPopupWindow() { return mInstance; }
protected abstract void initView();
protected abstract void initEvent();
protected void initWindow() {
mInstance.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mInstance.setOutsideTouchable(true);
mInstance.setTouchable(true);
}
public void showBashOfAnchor(View anchor, LayoutGravity layoutGravity, int xmerge, int ymerge) {
int[] offset=layoutGravity.getOffset(anchor, mInstance);
mInstance.showAsDropDown(anchor, offset[0]+xmerge, offset[1]+ymerge);
}
public void showAsDropDown(View anchor, int xoff, int yoff) {
mInstance.showAsDropDown(anchor, xoff, yoff);
}
public void showAtLocation(View parent, int gravity, int x, int y) {
mInstance.showAtLocation(parent, gravity, x, y);
}
}
這里我們要實(shí)現(xiàn)的就是“showBashOfAnchor”方法,其中有一個(gè)“LayoutGravity”類型的參數(shù),這就是控制PopupWindow相對(duì)錨點(diǎn)位置的對(duì)象。下面來定義“LayoutGravity”:
public static class LayoutGravity {
private int layoutGravity;
// waring, don't change the order of these constants!
public static final int ALIGN_LEFT=0x1;
public static final int ALIGN_ABOVE=0x2;
public static final int ALIGN_RIGHT=0x4;
public static final int ALIGN_BOTTOM=0x8;
public static final int TO_LEFT=0x10;
public static final int TO_ABOVE=0x20;
public static final int TO_RIGHT=0x40;
public static final int TO_BOTTOM=0x80;
public static final int CENTER_HORI=0x100;
public static final int CENTER_VERT=0x200;
public LayoutGravity(int gravity) {
layoutGravity=gravity;
}
public int getLayoutGravity() { return layoutGravity; }
public void setLayoutGravity(int gravity) { layoutGravity=gravity; }
public void setHoriGravity(int gravity) {
layoutGravity&=(0x2+0x8+0x20+0x80+0x200);
layoutGravity|=gravity;
}
public void setVertGravity(int gravity) {
layoutGravity&=(0x1+0x4+0x10+0x40+0x100);
layoutGravity|=gravity;
}
public boolean isParamFit(int param) {
return (layoutGravity & param) > 0;
}
public int getHoriParam() {
for(int i=0x1; i<=0x100; i=i<<2)
if(isParamFit(i))
return i;
return ALIGN_LEFT;
}
public int getVertParam() {
for(int i=0x2; i<=0x200; i=i<<2)
if(isParamFit(i))
return i;
return TO_BOTTOM;
}
public int[] getOffset(View anchor, PopupWindow window) {
int anchWidth=anchor.getWidth();
int anchHeight=anchor.getHeight();
int winWidth=window.getWidth();
int winHeight=window.getHeight();
View view=window.getContentView();
if(winWidth<=0)
winWidth=view.getWidth();
if(winHeight<=0)
winHeight=view.getHeight();
int xoff=0;
int yoff=0;
switch (getHoriParam()) {
case ALIGN_LEFT:
xoff=0; break;
case ALIGN_RIGHT:
xoff=anchWidth-winWidth; break;
case TO_LEFT:
xoff=-winWidth; break;
case TO_RIGHT:
xoff=anchWidth; break;
case CENTER_HORI:
xoff=(anchWidth-winWidth)/2; break;
default:break;
}
switch (getVertParam()) {
case ALIGN_ABOVE:
yoff=-anchHeight; break;
case ALIGN_BOTTOM:
yoff=-winHeight; break;
case TO_ABOVE:
yoff=-anchHeight-winHeight; break;
case TO_BOTTOM:
yoff=0; break;
case CENTER_VERT:
yoff=(-winHeight-anchHeight)/2; break;
default:break;
}
return new int[]{ xoff, yoff };
}
}
這里的主要方法就是“getOffset”,它會(huì)根據(jù)水平和垂直方向的gravity決定PopupWindow相對(duì)錨點(diǎn)的位置。
使用“LayoutGravity”時(shí),可以通過“setHoriGravity”和“setVertGravity”方法設(shè)置水平和垂直方向的gravity,或者新建一個(gè)“LayoutGravity”對(duì)象。
下面是一個(gè)demo:

使用setAnimationStyle方法添加動(dòng)畫
上面我們提到了為PopupWindow設(shè)置背景和注冊(cè)事件監(jiān)聽器,現(xiàn)在我們?cè)賮頌镻opupWindow添加動(dòng)畫。
這里的動(dòng)畫是指PopupWindow出現(xiàn)和消失時(shí)的動(dòng)畫。默認(rèn)是直接彈出和消失,這樣難免讓用戶有一種突兀的感覺;如果PopupWindow能夠“滑入”屏幕和“滑出”屏幕(或者其他方式),用戶體驗(yàn)會(huì)更好。
為PopupWindow添加動(dòng)畫可以調(diào)用`setAnimationStyle`方法,該方法只有一個(gè)參數(shù),就是指定動(dòng)畫的樣式,因此我們需要定義動(dòng)畫資源和樣式資源。
下面是一個(gè)“滑入滑出”動(dòng)畫:
<!-- res/anim/translate_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/translate_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>
然后定義“滑動(dòng)”動(dòng)畫樣式:
<!-- res/values/styles.xml --> <style name="animTranslate"> <item name="android:windowEnterAnimation">@anim/translate_in</item> <item name="android:windowExitAnimation">@anim/translate_out</item> </style>
現(xiàn)在我們就可以為PopupWindow添加“滑動(dòng)”動(dòng)畫了:
window.setAnimationStyle(R.style.animTranslate);
我們來看下效果:

PS:這里由于動(dòng)畫的時(shí)間太短(200ms),另外轉(zhuǎn)GIF的時(shí)候可能截取的頻率有點(diǎn)低,導(dǎo)致滑動(dòng)效果不是很明顯,建議自己運(yùn)行demo查看
現(xiàn)在PopupWindow的出現(xiàn)/消失已經(jīng)不是那么突兀了。不過,當(dāng)彈窗出現(xiàn)后,發(fā)現(xiàn)彈窗和背景不是很容易區(qū)分,如果此時(shí)彈窗的背景能“變暗”就好了。
沒問題,我們可以在彈窗出現(xiàn)后讓背景變暗,并在彈窗消失后讓背景還原:
window.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams lp=getWindow().getAttributes();
lp.alpha=1.0f;
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
getWindow().setAttributes(lp);
}
});
window.showAtLocation(activityPopup, Gravity.BOTTOM, 0, 0);
WindowManager.LayoutParams lp=getWindow().getAttributes();
lp.alpha=0.3f;
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
getWindow().setAttributes(lp);
現(xiàn)在再來看下效果:

現(xiàn)在PopupWindow就比較明顯了。
另外,我們還實(shí)現(xiàn)了透明度、縮放和旋轉(zhuǎn)三種動(dòng)畫樣式,實(shí)現(xiàn)方式和上述大同小異,這里就不再贅述。
源代碼
上述所有代碼(包括未給出的)都已上傳到GitHub:
https://github.com/jzyhywxz/PopupWindow
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Popupwindow彈出窗口的簡單使用方法
- Android中使用PopupWindow 仿微信點(diǎn)贊和評(píng)論彈出
- Android 使用PopupWindow實(shí)現(xiàn)彈出更多的菜單實(shí)例詳解
- Android popupwindow簡單使用方法介紹
- Android組件popupwindow使用方法詳解
- Android自定義彈出窗口PopupWindow使用技巧
- Android中PopupWindow使用方法詳解
- android使用PopupWindow實(shí)現(xiàn)頁面點(diǎn)擊頂部彈出下拉菜單
- Android PopupWindow使用實(shí)例
- PopupWindow使用方法詳解
相關(guān)文章
android開發(fā)基礎(chǔ)教程—打電話發(fā)短信
打電話發(fā)短信的功能已經(jīng)離不開我們的生活了,記下來介紹打電話發(fā)短信的具體實(shí)現(xiàn)代碼,感興趣的朋友可以了解下2013-01-01
彈出一個(gè)帶確認(rèn)和取消的dialog實(shí)例
下面小編就為大家?guī)硪黄獜棾鲆粋€(gè)帶確認(rèn)和取消的dialog實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03
Android Bitmap的加載優(yōu)化與Cache相關(guān)介紹
這篇文章主要介紹了Android中性能優(yōu)化之Bitmap的加載優(yōu)化與Cache相關(guān)內(nèi)容介紹,文中介紹的很詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-02-02
詳解Android App中使用VideoView來實(shí)現(xiàn)視頻播放的方法
這篇文章主要介紹了Android App中使用VideoView來實(shí)現(xiàn)視頻播放的方法,舉例講解了VideoView組件中setVidePath(String path)和setVideoURI(Uri uri)的用法,需要的朋友可以參考下2016-04-04
從零開始學(xué)android實(shí)現(xiàn)計(jì)算器功能示例分享(計(jì)算器源碼)
這篇文章主要介紹了android實(shí)現(xiàn)的計(jì)算器功能示例,可以加減乘除;可以倒退,可以清空文本,大家參考使用吧2014-02-02
Android BroadcastReceiver廣播簡單使用
這篇文章主要為大家詳細(xì)介紹了Android BroadcastReceiver廣播簡單的使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
Android UI設(shè)計(jì)與開發(fā)之PopupWindow仿騰訊新聞底部彈出菜單
這篇文章主要為大家詳細(xì)介紹了Android UI設(shè)計(jì)與開發(fā)之PopupWindow仿騰訊新聞底部彈出菜單,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08

