Android 中PopupWindow使用

简介:

Android 中PopupWindow使用。

PopupWindow会阻塞对话框,要在外部线程 或者 PopupWindow本身做退出才行。

mypopWindow.xml的Layout设计如下。

<?xml version= "1.0"  encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:layout_width= "fill_parent"
     android:layout_height= "fill_parent"
     android:background= "#259"
     android:orientation= "vertical"  >
 
     <TextView
         android:id= "@+id/textView1"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "Here is Pop Window"  />
 
     <Button
         android:id= "@+id/button1"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "OK"  />
 
     <Button
         android:id= "@+id/button2"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "Cancle"  />
 
</LinearLayout>

 MainActivity.java文件。

 在MainActivity的Button按钮单击,然后显示PopupWindow。

private  void  showPopWindow(Context context, View parent)
     {      
         LayoutInflater inflater = (LayoutInflater)             
                 context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
         final  View vPopWindow=inflater.inflate(R.layout.mypopwindow, null , false ); 
         //宽300 高300           
         final  PopupWindow popWindow = new  PopupWindow(vPopWindow, 300 , 300 , true );
         Button okButton = (Button)vPopWindow.findViewById(R.id.button1);
         okButton.setOnClickListener( new  View.OnClickListener() {
             
             @Override
             public  void  onClick(View v) {
                 // TODO Auto-generated method stub
                 Toast.makeText(MainActivity. this , "You click OK" , Toast.LENGTH_SHORT).show();
             }
         });
         
         Button cancleButton = (Button)vPopWindow.findViewById(R.id.button2);
         cancleButton.setOnClickListener( new  View.OnClickListener() {
             
             @Override
             public  void  onClick(View v) {
                 // TODO Auto-generated method stub
                 popWindow.dismiss(); //Close the Pop Window
             }
         });
         
         popWindow.showAtLocation(parent, Gravity.CENTER, 0 , 0 );
         
         
     }

 效果图如下:最后显示是剧中的,这里我只是截取了部分图片。

单击Ok按钮显示You click OK信息。单击取消,则关闭PopupWindow.



本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2013/03/15/2961430.html,如需转载请自行联系原作者

目录
相关文章
|
Android开发
Android 封装一个通用的PopupWindow
`PopupWindow`这个类用来实现一个弹出框,可以使用任意布局的`View`作为其内容,这个弹出框是悬浮在当前`Activity`之上的,一般`PopupWindow`的使用
231 0
|
XML JSON Java
android 继承popupWindow实现时间、地址选择器
日期选择、地址选择,都是开发中少不了的功能,下面通过自定义的形式,同一套代码实现时间选择与地址选择,通过构造方法的不同来实现。
445 0
android 继承popupWindow实现时间、地址选择器
|
Android开发
Android 7.0及以上PopupWindow 适配问题
Android 7.0及以上PopupWindow 适配问题
107 0
|
XML Android开发 iOS开发
Android 仿IOS的PopupWindow和通用BasePopupWindow搭建
截图 pw.png 实现 1、BasePopupWindow.java 1.1、实现动态加载不同layout 1.2、动态配置是否弹出后背景半透明,关闭时候恢复(监听ondismiss,靠window类来变色) 1.
2346 0
|
Android开发
Android项目实战(十七):QQ空间实现(二)—— 分享功能 / 弹出PopupWindow
原文:Android项目实战(十七):QQ空间实现(二)—— 分享功能 / 弹出PopupWindow 这是一张QQ空间说说详情的截图。 分析: 1、点击右上角三个点的图标,在界面底部弹出一个区域,这个区域有一些按钮提供给我们操作 2、当该区域出现的时候,详情界面便灰了,也说成透明度变...
888 0
|
Android开发
Android弹窗二则: PopupWindow和AlertDialog
前言 弹窗是图形界面必备的一个模块, 回忆一下windows那些恶心爆了的错误弹窗吧, 把弹窗制作的更高效友好一点是非常必要的. 这里说两个常用的弹窗类, PopupWindow和AlertDialog.
1122 0