Android PopupWindow詳解

yanxing 8年前發布 | 10K 次閱讀 Android開發 移動開發

來自: http://www.jcodecraeer.com//a/anzhuokaifa/androidkaifa/2014/0702/1627.html


構造方法

public PopupWindow(View contentView, int width, int height, boolean focusable)

contentView為要顯示的view,width和height為寬和高,值為像素值,也可以是MATCHT_PARENT和WRAP_CONTENT。

還可以

public PopupWindow (Context context)

或者

public PopupWindow(View contentView, int width, int height)

或者

public PopupWindow(View contentView)

其中第一種最省事,構造函數中設置了要顯示的View,寬度 ,高度以及是否能獲得焦點。以上是幾種用的比較常見的構造方法。


改變PopupWindow的視圖內容

可以通過

public void setContentView(View contentView)

來改變popup的顯示內容,也可以用來初始化PopupWindow的View,比如使用構造函數public PopupWindow (Context context)獲得的Popupwindow就只能用setContentView來設置內容。

PopupWindow popupWindow = new PopupWindow(context);
popupWindow.setContentView(contentview);


獲得PopupWindow的視圖內容

public View getContentView()


顯示PopupWindow

  • showAsDropDown(View anchor):相對某個控件的位置(正左下方),無偏移

  • showAsDropDown(View anchor, int xoff, int yoff):相對某個控件的位置,有偏移

  • showAtLocation(View parent, int gravity, int x, int y):相對于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以設置偏移或無偏移

其實我發現showAtLocation的parent參數可以很隨意,只要是activity中的view都可以。


大小:

有兩種方法設置PopupWindow的大小:

調用有寬高參數的構造函數:

LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentview = inflater.inflate(R.layout.popup_process, null);
PopupWindow popupWindow = new PopupWindow(contentview,LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);

通過setWidthsetHeight設置

PopupWindow popupWindow = new PopupWindow(contentview);
popupWindow.setWidth(LayoutParams.WRAP_CONTENT);           
popupWindow.setHeight(LayoutParams.WRAP_CONTENT);

兩種辦法是等效的,不管采用何種辦法,必須設置寬和高,否則不顯示任何東西.

這里的WRAP_CONTENT可以換成fill_parent 也可以是具體的數值,它是指PopupWindow的大小,也就是contentview的大小,注意popupwindow根據這個大小顯示你的View,如果你的View本身是從xml得到的,那么xml的第一層view的大小屬性將被忽略。相當于popupWindowwidthheight屬性直接和第一層View相對應。

設想下面一種場景:

popupWindow 設置為WRAP_CONTENT ,我想得到的是一個寬150dip 80dippopupwindow,需要額外加一層

LinearLayout ,這個LinearLayout layout_widthlayout_height為任意值。而我們真正想顯示的View 放在第二層,并且  android:layout_width="150.0dip"  android:layout_height="80.0dip"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
>
    <LinearLayout
        android:background="@drawable/shape_ret_loading_bg"
        android:layout_width="150.0dip"
        android:layout_height="80.0dip"
        android:orientation="vertical"
        android:gravity="center"
    >
        <TextView
            android:textSize="14dip"
            android:textColor="@color/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10.0dip"
            android:text="加載中..."
        />
    </LinearLayout>
</LinearLayout>


PopUpWindow的焦點:

setFocusable設置PopupWindow的焦點,一般資料對此的解釋都是:是否讓Popupwindow可以點擊但是這揭示了本質卻與直觀現象不符。實際上,

如果:

setFocusable(true);

PopUpWindow本身可以看作一個類似于模態對話框的東西(但有區別),PopupWindow彈出后,所有的觸屏和物理按鍵都有PopupWindows處理。其他任何事件的響應都必須發生在PopupWindow消失之后, (home  等系統層面的事件除外)。比如這樣一個PopupWindow出現的時候,按back鍵首先是讓PopupWindow消失,第二次按才是退出activity,準確的說是想退出activity你得首先讓PopupWindow消失,因為不并是任何情況下按back  PopupWindow都會消失,必須在PopupWindow設置了背景的情況下 。

如果PopupWindow中有Editor的話,focusable要為true。

setFocusable(false);

PopUpWindow只是一個浮現在當前界面上的view而已,不影響當前界面的任何操作。

是一個“沒有存在感”的東西。

一般情況下setFocusable(true);


點擊空白處的時候讓PopupWindow消失

關于PopupWindow最搞笑的地方是setOutsideTouchable方法,原本以為如果你setOutsideTouchable(true)則點擊PopupWindow之外的地方PopupWindow會消失,其實這玩意兒好像一點用都沒有。

要讓點擊PopupWindow之外的地方PopupWindow消失你需要調用setBackgroundDrawable(new BitmapDrawable());

設置背景,為了不影響樣式,這個背景是空的。還可以這樣寫,覺得這樣要保險些:

setBackgroundDrawable(new ColorDrawable(0x00000000));

背景不為空但是完全透明。如此設置還能讓PopupWindow在點擊back的時候消失。其實一直覺得很奇怪,不明白為什么一個背景會影響點擊事件,只知道這樣用可行。

后來在評論中有人回答了此問題:


如果有背景,則會在contentView外面包一層PopupViewContainer之后作為mPopupView,如果沒有背景,則直接用contentView作為mPopupView。

而這個PopupViewContainer是一個內部私有類,它繼承了FrameLayout,在其中重寫了Key和Touch事件的分發處理

詳情見:http://www.cnblogs.com/mengdd/p/3569127.html 



PopupWindow實現一個真正的模態對話框

剛才說到“popupWindow.setFocusable(true);

PopUpWindow本身可以看作一個類似于模態對話框的東西

其實這句話有一些細微的錯誤,在android中一個模態對話框應該是這樣的:

阻止屏幕上的其他View事件,且點擊PopupWindow外面不會消失,但是能響應back事件(點擊back消失),所以如果要讓

PopupWindow有模態對話框的表現,則不能調用setBackgroundDrawable,因為setBackgroundDrawable會讓點擊PopupWindow外面PopupWindow消失。但是如果不調用setBackgroundDrawable則點擊back鍵也不會消失,比模態對話框還變態。不過還是有解決的辦法:

LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentview = inflater.inflate(R.layout.popup_process, null);
contentview.setFocusable(true); // 這個很重要
contentview.setFocusableInTouchMode(true);
final PopupWindow popupWindow = new PopupWindow(contentview,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(false);
contentview.setOnKeyListener(new OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            popupWindow.dismiss();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
            return true;
        }
        return false;
    }
});
popupWindow.showAtLocation(view,  Gravity.CENTER|Gravity.CENTER_HORIZONTAL, 0, 0);

其中要注意的是PopupWindow 的視圖contentview的設置,contentview.setFocusable(true);  這個很重要,注意不是PopupWindowsetFocusable,同時setOnKeyListener也是contentview調用的,而不是PopupWindowsetOnKeyListener方法中的代碼很好理解,但是為啥是contentview調用就不是很清楚了,貌似沒人去研究。其實為了是代碼的相關性更低contentview.setOnKeyListener可以用popupWindow.getContentView.setOnKeyListener來代替。

話說如果PopupWindow需要如此這番周折才能實現一個模態對話框何不直接用Dialog呢?其實我只是將這些現象講清楚,你可以根據這些知識將PopupWindow 應用在很苛刻的場景中。


PopupWindow的動畫

很多時候我們把PopupWindow用作自定義的菜單,需要一個從底部向上彈出的效果,這就需要為PopupWindow添加動畫。

設置動畫的方法:

public void setAnimationStyle(int animationStyle)

在res/value/styles.xml添加一個sytle

<style name="anim_menu_bottombar">
    <item name="android:windowEnterAnimation">@anim/menu_bottombar_in</item>
    <item name="android:windowExitAnimation">@anim/menu_bottombar_out</item>
</style>

在工程res下新建anim文件夾,在anim文件夾先新建兩個xml文件

menu_bottombar_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="250"
        android:fromYDelta="100.0%"
        android:toYDelta="0.0" />
</set>

menu_bottombar_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="250"
        android:fromYDelta="0.0"
        android:toYDelta="100%" />
</set>

mPopupWindow.setAnimationStyle(R.style.menu_anim_bottombar);


關于Popupwindow介紹的差不多了,如果你覺得還需要補充請在下面留言。

 本文由用戶 yanxing 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!