ITeye博客

Android 中 PopupWindow的用法 汇总 Android 中 PopupWindow的用法 汇总

mmdev · 2013-01-18 17:49 · Markdown

Android 中 PopupWindow的用法 汇总

2012-10-12 10:16241人阅读评论(0)收藏举报

在Android中有很多级别的Window,不同级别的Window按照z-index方向分布。下面看看Android控件(view)PopupWindow的用法(位置、动画、焦点)。

1、创建PopouWindow及相关参数设置

[html]view plaincopyprint?

  1. //创建一个包含自定义view的PopupWindow
  2. ivatePopupWindowmakePopupWindow(Contextcx){
  3. PopupWindowwindow;
  4. window=newPopupWindow(cx);
  5. TextViewcontentView=newTextView(cx);
  6. contentView.setGravity(Gravity.CENTER);
  7. finalResourcesres=cx.getResources();
  8. //contentView.setBackgroundColor(R.color.page_window_bgcolor);
  9. //window.setBackgroundDrawable(new
  10. //ColorDrawable(res.getColor(R.color.page_window_bgcolor)));
  11. contentView.setLayoutParams(newLayoutParams(LayoutParams.WRAP_CONTENT,
  12. LayoutParams.WRAP_CONTENT));
  13. //设置PopupWindow显示和隐藏时的动画
  14. window.setAnimationStyle(R.style.AnimationFade);
  15. //设置PopupWindow的大小(宽度和高度)
  16. window.setWidth(res.getDimensionPixelSize(R.dimen.page_window_width));
  17. window.setHeight(res.getDimensionPixelSize(R.dimen.page_window_height));
  18. //设置PopupWindow的内容view
  19. window.setContentView(contentView);
  20. //设置PopupWindow外部区域是否可触摸
  21. window.setOutsideTouchable(true);
  22. returnwindow

2、PopupWindow显示和隐藏的动画设置
XML配置代码:

[html]view plaincopyprint?

  1. <!--PopupWindow窗口淡入淡出动画-->
  2. <stylename="AnimationFade">
  3. <itemname="android:windowEnterAnimation">@anim/fade_in</item>
  4. <itemname="android:windowExitAnimation">@anim/fade_out</item>
  5. </style>

JAVA代码:

[html]view plaincopyprint?

  1. window.setAnimationStyle(R.style.AnimationFade);

3、PopupWindow的焦点设置

[html]view plaincopyprint?

  1. window.setFocusable(true);//设置PopupWindow可获得焦点
  2. window.setTouchable(true);//设置PopupWindow可触摸
  3. window.setOutsideTouchable(true);//设置非PopupWindow区域可触摸

4、PopupWindow的显示及显示位置设置

[html]view plaincopyprint?

  1. window.showAtLocation();

[html]view plaincopyprint?

  1. window.showAsDropDown();

5、PopupWindow的隐藏

[html]view plaincopyprint?

  1. finalPopupWindowwindow=mPageStatWin;
  2. if(null!=window&&window.isShowing()){
  3. win.dismiss();
  4. }

6、PopupWindow的显示及位置设置

[html]view plaincopyprint?

  1. window.showAtLocation(parent,Gravity.RIGHT|Gravity.BOTTOM,10,10);

第一个参数指定PopupWindow的锚点view,即依附在哪个view上。
第二个参数指定起始点为parent的右下角,第三个参数设置以parent的右下角为原点,向左、上各偏移10像素。

[html]view plaincopyprint?

  1. //将PopupWindow作为anchor的下拉窗口显示。即在anchor的左下角显示
  2. window.showAsDropDown(anchor);
  3. //xoff,yoff基于anchor的左下角进行偏移。
  4. window.showAsDropDown(anchor,xoff,yoff);

如果没有充足的空间显示PopupWindow,那么PopupWindow的左下角将位于anchor的左上角来显示。

在Android中有很多级别的Window,不同级别的Window按照z-index方向分布。下面看看Android控件(view)PopupWindow的用法(位置、动画、焦点)。

1、创建PopouWindow及相关参数设置

[html]view plaincopyprint?

  1. //创建一个包含自定义view的PopupWindow
  2. ivatePopupWindowmakePopupWindow(Contextcx){
  3. PopupWindowwindow;
  4. window=newPopupWindow(cx);
  5. TextViewcontentView=newTextView(cx);
  6. contentView.setGravity(Gravity.CENTER);
  7. finalResourcesres=cx.getResources();
  8. //contentView.setBackgroundColor(R.color.page_window_bgcolor);
  9. //window.setBackgroundDrawable(new
  10. //ColorDrawable(res.getColor(R.color.page_window_bgcolor)));
  11. contentView.setLayoutParams(newLayoutParams(LayoutParams.WRAP_CONTENT,
  12. LayoutParams.WRAP_CONTENT));
  13. //设置PopupWindow显示和隐藏时的动画
  14. window.setAnimationStyle(R.style.AnimationFade);
  15. //设置PopupWindow的大小(宽度和高度)
  16. window.setWidth(res.getDimensionPixelSize(R.dimen.page_window_width));
  17. window.setHeight(res.getDimensionPixelSize(R.dimen.page_window_height));
  18. //设置PopupWindow的内容view
  19. window.setContentView(contentView);
  20. //设置PopupWindow外部区域是否可触摸
  21. window.setOutsideTouchable(true);
  22. returnwindow

2、PopupWindow显示和隐藏的动画设置
XML配置代码:

[html]view plaincopyprint?

  1. <!--PopupWindow窗口淡入淡出动画-->
  2. <stylename="AnimationFade">
  3. <itemname="android:windowEnterAnimation">@anim/fade_in</item>
  4. <itemname="android:windowExitAnimation">@anim/fade_out</item>
  5. </style>

JAVA代码:

[html]view plaincopyprint?

  1. window.setAnimationStyle(R.style.AnimationFade);

3、PopupWindow的焦点设置

[html]view plaincopyprint?

  1. window.setFocusable(true);//设置PopupWindow可获得焦点
  2. window.setTouchable(true);//设置PopupWindow可触摸
  3. window.setOutsideTouchable(true);//设置非PopupWindow区域可触摸

4、PopupWindow的显示及显示位置设置

[html]view plaincopyprint?

  1. window.showAtLocation();

[html]view plaincopyprint?

  1. window.showAsDropDown();

5、PopupWindow的隐藏

[html]view plaincopyprint?

  1. finalPopupWindowwindow=mPageStatWin;
  2. if(null!=window&&window.isShowing()){
  3. win.dismiss();
  4. }

6、PopupWindow的显示及位置设置

[html]view plaincopyprint?

  1. window.showAtLocation(parent,Gravity.RIGHT|Gravity.BOTTOM,10,10);

第一个参数指定PopupWindow的锚点view,即依附在哪个view上。
第二个参数指定起始点为parent的右下角,第三个参数设置以parent的右下角为原点,向左、上各偏移10像素。

[html]view plaincopyprint?

  1. //将PopupWindow作为anchor的下拉窗口显示。即在anchor的左下角显示
  2. window.showAsDropDown(anchor);
  3. //xoff,yoff基于anchor的左下角进行偏移。
  4. window.showAsDropDown(anchor,xoff,yoff);

如果没有充足的空间显示PopupWindow,那么PopupWindow的左下角将位于anchor的左上角来显示。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。