1.使用 popwindow实现如下效果:

Android中 Popwindow的使用_第1张图片

2.代码如下:MainActivity.java

[java] view plain copy print ?
  1. packagemtpop.window.main;
  2. importandroid.app.Activity;
  3. importandroid.content.Context;
  4. importandroid.content.res.Resources;
  5. importandroid.os.Bundle;
  6. importandroid.view.Gravity;
  7. importandroid.view.LayoutInflater;
  8. importandroid.view.View;
  9. importandroid.view.View.OnClickListener;
  10. importandroid.view.ViewGroup.LayoutParams;
  11. importandroid.widget.Button;
  12. importandroid.widget.LinearLayout;
  13. importandroid.widget.PopupWindow;
  14. importandroid.widget.PopupWindow.OnDismissListener;
  15. importandroid.widget.TextView;
  16. publicclassMainActivityextendsActivity
  17. {
  18. privatestaticfinalStringTAG="MainActivity";
  19. privateButtonbutton;
  20. @Override
  21. publicvoidonCreate(BundlesavedInstanceState)
  22. {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.main);
  25. button=(Button)findViewById(R.id.button);
  26. button.setOnClickListener(newOnClickListener()
  27. {
  28. @Override
  29. publicvoidonClick(Viewv)
  30. {
  31. //显示popupWindow
  32. PopupWindowpopupWindow=makePopupWindow(MainActivity.this);
  33. int[]xy=newint[2];
  34. button.getLocationOnScreen(xy);
  35. popupWindow.showAtLocation(button,Gravity.RIGHT|Gravity.TOP,-xy[0]/2,xy[1]+button.getWidth());
  36. //popupWindow.showAsDropDown(button,0,0);
  37. }
  38. });
  39. }
  40. //创建一个包含自定义view的PopupWindow
  41. privatePopupWindowmakePopupWindow(Contextcx)
  42. {
  43. PopupWindowwindow;
  44. window=newPopupWindow(cx);
  45. //ViewcontentView=LayoutInflater.from(this).inflate(R.layout.popwindow,null);
  46. //window.setContentView(contentView);
  47. Buttonb1=newButton(this);
  48. b1.setText("first");
  49. b1.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
  50. Buttonb2=newButton(this);
  51. b2.setText("Second");
  52. b2.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
  53. LinearLayoutlinearLayout=newLinearLayout(this);
  54. linearLayout.addView(b1);
  55. linearLayout.addView(b2);
  56. linearLayout.setOrientation(LinearLayout.VERTICAL);
  57. window.setContentView(linearLayout);
  58. window.setBackgroundDrawable(getResources().getDrawable(R.drawable.pop_bg));
  59. window.setWidth(DisplayManager.dipToPixel(150));
  60. window.setHeight(DisplayManager.dipToPixel(150));
  61. //设置PopupWindow外部区域是否可触摸
  62. window.setFocusable(true);//设置PopupWindow可获得焦点
  63. window.setTouchable(true);//设置PopupWindow可触摸
  64. window.setOutsideTouchable(true);//设置非PopupWindow区域可触摸
  65. returnwindow;
  66. }
  67. }

3. main.xml

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <LinearLayout
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:background="@android:color/darker_gray"
  10. android:orientation="horizontal">
  11. <TextView
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:layout_weight="1"
  15. android:text="Title"/>
  16. <Button
  17. android:id="@+id/button"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="click"/>
  21. </LinearLayout>
  22. </LinearLayout>

注意:

*新建一个popupWindow弹出框popupWindow是一个阻塞式的弹出框,这就意味着在我们退出这个弹出框之前,程序会一直等待,

*这和AlertDialog不同哦,AlertDialog是非阻塞式弹出框,AlertDialog弹出的时候,后台可是还可以做其他事情的哦。 *

******************************************************************

1.PopupWindow的隐藏

[java] view plain copy print ?
  1. finalPopupWindowwindow=mPageStatWin;
  2. if(null!=window&&window.isShowing()){
  3. win.dismiss();
  4. }

2.Popupwindow的显示及位置设置

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


第一个参数指定PopupWindow的锚点view,即依附在哪个view上。

第二个参数指定起始点为parent的右下角,第三个参数设置以parent的右下角为原点,向左、上各偏移10像素。


//将PopupWindow作为anchor的下拉窗口显示。即在anchor的左下角显示
window. showAsDropDown (anchor ) ;
//xoff,yoff基于anchor的左下角进行偏移。
window. showAsDropDown (anchor, xoff, yoff ) ; 如果没有充足的空间显示PopupWindow,那么PopupWindow的左下角将位于anchor的左上角来显示。


*popupWindow.showAsDropDown(Viewview)弹出对话框,位置在紧挨着view组件

*showAsDropDown(Viewanchor,intxoff,intyoff)弹出对话框,位置在紧挨着view组件,xy代表着偏移量

*showAtLocation(Viewparent,intgravity,intx,inty)弹出对话框

*parent父布局gravity依靠父布局的位置如Gravity.CENTERxy坐标值

[java] view plain copy print ?
  1. window.setWidth(DisplayManager.dipToPixel(150));
  2. window.setHeight(DisplayManager.dipToPixel(150));
  3. //设置PopupWindow外部区域是否可触摸
  4. window.setFocusable(true);//设置PopupWindow可获得焦点
  5. window.setTouchable(true);//设置PopupWindow可触摸
  6. window.setOutsideTouchable(true);//设置非PopupWindow区域可触摸
设置其可以获得焦点,可触摸,外部区域可触摸(很重要)

更多相关文章

  1. Android通过基站获取地理位置
  2. edittext获取焦点并弹出软键盘
  3. Android 铃声设置界面,起始位置为当前已选项
  4. android Edittext焦点获取和清除
  5. MTK平台修改音量默认值需要改动的位置
  6. 如何在Android 11 中正确请求位置权限?以及Android 8 - 11位置权
  7. Android中PopupWindow的用法(位置、动画、焦点)
  8. Android中利用SpannableString实现点击同一按钮(Button)不同位置
  9. Android 记住listView的位置

随机推荐

  1. Android(安卓)7.1预编译编译第三方so
  2. Spinner
  3. Android(安卓)NullPointerException解决
  4. [置顶] Android中以JAR形式封装控件或者
  5. Android开发平台Android(安卓)Studio学习
  6. android的listView组件
  7. Android入门教程 (二) 第一个App HelloWorl
  8. 分享17个老罗Android开发视频教程(免费下
  9. android实现应用程序的开机自启动
  10. 简单明了的分析Android触摸事件,看完再也