Android UI开发第三篇-PopupWindow

来源:互联网 发布:超级优化还原 编辑:程序博客网 时间:2024/06/11 05:19

 PopupWindow在android.widget包下,弹出窗口的形式展示。官方文档对该控件的描述是:“一个弹出窗口控件,可以用来显示任意视图(View),而且会浮动在当前 活动(activity)的顶部”。PopupWindow可以让我们实现多种自定义控件,例如:menu、alertdialog等弹窗似的View。


    Popupwindow需要在onCreate时初始化、通过触发事件展示出来。


[java] view plaincopyprint?
  1. public class ShowPopupWindow extends Activity implements View.OnClickListener {  
  2.   
  3.     View view;  
  4.     PopupWindow pop;  
  5.     Button btnShowAsDrawDown;  
  6.     Button btnShowAsDrawDown1;  
  7.     Button btnShowAtLocation;  
  8.   
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         // TODO Auto-generated method stub  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.popup_activity);  
  14.         btnShowAsDrawDown = (Button) findViewById(R.id.btnShowAsDrawDown);  
  15.         btnShowAsDrawDown.setOnClickListener(this);  
  16.         btnShowAsDrawDown1 = (Button) findViewById(R.id.btnShowAsDrawDown1);  
  17.         btnShowAsDrawDown1.setOnClickListener(this);  
  18.         btnShowAtLocation = (Button) findViewById(R.id.btnShowAt);  
  19.         btnShowAtLocation.setOnClickListener(this);  
  20.         initPopupWindow();  
  21.     }  
  22.   
  23.     @Override  
  24.     public void onClick(View v) {  
  25.         // TODO Auto-generated method stub  
  26.         switch (v.getId()) {  
  27.         case R.id.btnShowAsDrawDown:  
  28.             if (pop.isShowing()) {  
  29.                 pop.dismiss();  
  30.             } else {  
  31.                 pop.showAsDropDown(v);  
  32.             }  
  33.             break;  
  34.         case R.id.btnShowAsDrawDown1:  
  35.             if (pop.isShowing()) {  
  36.                 pop.dismiss();  
  37.             } else {  
  38.                 pop.showAsDropDown(v, 0, -160);  
  39.             }  
  40.             break;  
  41.         default:  
  42.             if (pop.isShowing()) {  
  43.                 pop.dismiss();  
  44.             } else {  
  45.                 pop.showAtLocation(findViewById(R.id.main),  
  46.                         Gravity.CENTER_HORIZONTAL, 00);  
  47.             }  
  48.             break;  
  49.         }  
  50.   
  51.     }  
  52.   
  53.     private void initPopupWindow() {  
  54.         view = this.getLayoutInflater().inflate(R.layout.popup_window, null);  
  55.         pop = new PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT,  
  56.                 ViewGroup.LayoutParams.WRAP_CONTENT);  
  57.         pop.setOutsideTouchable(true);  
  58.         view.setOnClickListener(new View.OnClickListener() {  
  59.             @Override  
  60.             public void onClick(View v) {  
  61.                 // TODO Auto-generated method stub  
  62.                 pop.dismiss();  
  63.             }  
  64.         });  
  65.     }  
  66.   
  67. }  



popup_activity.xml

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/main"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="pop demo!" />  
  12.   
  13.     <Button  
  14.         android:id="@+id/btnShowAsDrawDown"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="Show as drawndown(one parameter)" />  
  18.   
  19.     <Button  
  20.         android:id="@+id/btnShowAsDrawDown1"  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:text="Show as drawndown(three parameters)" />  
  24.   
  25.     <Button  
  26.         android:id="@+id/btnShowAt"  
  27.         android:layout_width="fill_parent"  
  28.         android:layout_height="wrap_content"  
  29.         android:text="Show At Location" />  
  30.   
  31. </LinearLayout>  


popup_window.xml

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="#d3d3d3"  
  6.     android:gravity="center_horizontal"  
  7.     android:orientation="vertical" >  
  8.   
  9.     <Button  
  10.         android:id="@+id/btn_pop"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_margin="10dip"  
  14.         android:text="Close" />  
  15.   
  16. </LinearLayout>  
0 0
原创粉丝点击