PopupWindow實例解析

今天結合自己在項目開發中的使用,對PopupWindow做一下總結,并對PopupWindow與Dialog做一下對比分析。

PopupWindow使用

  private void showPopwindow() {
        // 獲取自定義布局文件activity_popupwindow_left.xml的視圖
        View popupWindow_view = getLayoutInflater().inflate(R.layout.layout_subject, null, false);
        // 創建PopupWindow實例,200,LayoutParams.MATCH_PARENT分別是寬度和高度
        popupWindow = new PopupWindow(popupWindow_view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        popupWindow.setBackgroundDrawable(dw);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(false);
        popupWindow.setAnimationStyle(R.style.PopupAnimation);
        // 設置動畫效果
        RecyclerView recylerView = (RecyclerView) popupWindow_view.findViewById(R.id.recylerView);
        recylerView.setHasFixedSize(true);
        recylerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        recylerView.setAdapter(new SubjectAdapter());
        // 點擊其他地方消失
        popupWindow_view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    popupWindow = null;
                }
                return false;
            }
        });

    }

getLayoutInflater()引入自定義布局;
PopupWindow構造方法來設置寬高;
setBackgroundDrawable()設置背景;
setFocusable(true)獲取焦點;
setOutsideTouchable(true)觸摸屏幕其他位置消失;
setAnimationStyle(int)設置動畫
PopupWindow背景透明度

/** * 設置添加屏幕的背景透明度 * * @param bgAlpha */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        getWindow().setAttributes(lp);
    }

PopupWindow顯示位置

showAsDropDown(View anchor):相對某個控件的位置(正左下方),無偏移。
showAsDropDown(View anchor, int xoff, int yoff):相對某個控件的位置,有偏移。
showAtLocation(View parent, int gravity, int x, int y):相對于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以設置偏移或無偏移。

PopupWindow與Dialog的區別

1.PopupWindow是阻塞線程的,AlertDialog是非阻塞線程的。就是說AlertDialog在顯示時線程還在運行,PopupWindow顯示時線程陷入等待,直到dismiss才會繼續。

2.Dialog沒法設置寬為整個屏幕寬,總有點邊界。Popupwindow可以(PopupWindow也可以設置有邊界)。

不管是dialog還是popwindow,在調用ondestroy()時都要dismiss,否則會報錯。

自定義AlertDialog小結

 myDialog = new AlertDialog.Builder(MainActivity.this).create(); 
                myDialog.show(); 
                Window window = myDialog .getWindow();
window.setWindowAnimations(R.style.dialog_anim);
                myDialog.getWindow().setContentView(R.layout.mydialog); 
                myDialog.getWindow()  
                    .findViewById(R.id.button_back_mydialog)  
                    .setOnClickListener(new View.OnClickListener() {  
                    @Override  
                    public void onClick(View v) {  
                        myDialog.dismiss(); 
                    }  
                }); 

setContentView()加載自定義布局;
setWindowAnimations()設置動畫;

設置大小可以通過布局來設置,也可以動態設置

 //設置大小  
        WindowManager.LayoutParams layoutParams =     dialog.getWindow().getAttributes(); 
        layoutParams.width = 200; 
        layoutParams.height = LayoutParams.WRAP_CONTENT; 

顯示位置
方式一:

window.setGravity(Gravity.TOP); 

方式二:

AlertDialog dlg = new AlertDialog.Builder(this).create();
//-------------------------------------------------------------------
Window w=dlg.getWindow();
WindowManager.LayoutParams lp =w.getAttributes();
lp.x=10;
lp.y=150;

恩,總結到此!

 

來自: http://blog.csdn.net/qq_16131393/article/details/51597326

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