PopupWindow实例解析

隔壁小王 8年前
   <p>今天结合自己在项目开发中的使用,对PopupWindow做一下总结,并对PopupWindow与Dialog做一下对比分析。</p>    <p><strong>PopupWindow使用</strong></p>    <pre>  <code class="language-java">  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;              }          });        }</code></pre>    <p>getLayoutInflater()引入自定义布局;<br> PopupWindow构造方法来设置宽高;<br> setBackgroundDrawable()设置背景;<br> setFocusable(true)获取焦点;<br> setOutsideTouchable(true)触摸屏幕其他位置消失;<br> setAnimationStyle(int)设置动画<br> PopupWindow背景透明度</p>    <pre>  <code class="language-java">/** * 设置添加屏幕的背景透明度 * * @param bgAlpha */      public void backgroundAlpha(float bgAlpha) {          WindowManager.LayoutParams lp = getWindow().getAttributes();          lp.alpha = bgAlpha; //0.0-1.0          getWindow().setAttributes(lp);      }</code></pre>    <p><strong>PopupWindow显示位置</strong></p>    <p>showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移。<br> showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移。<br> showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移。</p>    <p><strong>PopupWindow与Dialog的区别</strong></p>    <p>1.PopupWindow是阻塞线程的,AlertDialog是非阻塞线程的。就是说AlertDialog在显示时线程还在运行,PopupWindow显示时线程陷入等待,直到dismiss才会继续。</p>    <p>2.Dialog没法设置宽为整个屏幕宽,总有点边界。Popupwindow可以(PopupWindow也可以设置有边界)。</p>    <p>不管是dialog还是popwindow,在调用ondestroy()时都要dismiss,否则会报错。</p>    <p><strong>自定义AlertDialog小结</strong></p>    <pre>  <code class="language-java"> 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();                       }                    }); </code></pre>    <p>setContentView()加载自定义布局;<br> setWindowAnimations()设置动画;</p>    <p>设置大小可以通过布局来设置,也可以动态设置</p>    <pre>  <code class="language-java"> //设置大小            WindowManager.LayoutParams layoutParams =     dialog.getWindow().getAttributes();           layoutParams.width = 200;           layoutParams.height = LayoutParams.WRAP_CONTENT; </code></pre>    <p>显示位置<br> 方式一:</p>    <pre>  <code class="language-java">window.setGravity(Gravity.TOP); </code></pre>    <p>方式二:</p>    <pre>  <code class="language-java">AlertDialog dlg = new AlertDialog.Builder(this).create();  //-------------------------------------------------------------------  Window w=dlg.getWindow();  WindowManager.LayoutParams lp =w.getAttributes();  lp.x=10;  lp.y=150;</code></pre>    <p>恩,总结到此!</p>    <p> </p>    <p>来自: <a href="/misc/goto?guid=4959674336993322746" rel="nofollow">http://blog.csdn.net/qq_16131393/article/details/51597326</a></p>