Android中在activity中弹出一个popwindow

//-----在onCreate方法--中------创建popwindow布局   --pop_item--------------------------
        View view=LayoutInflater.from(this).inflate(R.layout.pop_item, null);
        //找到两个按钮控件   登录 和我的
        bt_pop_login = (Button) view.findViewById(R.id.bt_pop_login);
        bt_pop_wode = (Button) view.findViewById(R.id.bt_pop_wode);
        //创建popwindow的大小
        // pop = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, false);  
        //创建popwindow的大小
        pop = new PopupWindow(view, 120, 200);  
        
        // 需要设置一下此参数,点击外边可消失  
        pop.setBackgroundDrawable(new BitmapDrawable());  
        //设置点击窗口外边窗口消失  
        pop.setOutsideTouchable(true);  
        // 设置此参数获得焦点,否则无法点击  
        pop.setFocusable(true);
        //可以设置  登录 和我的   的监听
        bt_pop_login.setOnClickListener(this);
        bt_pop_wode.setOnClickListener(this);

 

 

//-----------判断--popwindow是否展示----------------------------

if(pop.isShowing()) {  
                    // 隐藏窗口,如果设置了点击窗口外小时即不需要此方式隐藏  
                    pop.dismiss();  
                } else {  
                    // 显示窗口  
                    pop.showAsDropDown(v);  
                }  
                
                int [] location=new int [2];
                v.getLocationOnScreen(location);
                
                pop.showAtLocation(tv_wode, Gravity.NO_GRAVITY, location[0], location[1]-pop.getHeight());
               // pop.showAtLocation(v, Gravity.TOP, 20, 20);

 

//------------pop_item.xml  文件布局中-------------------------

<Button  
        android:id="@+id/bt_pop_login"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="登录" />    
    <Button  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="我的"
        android:id="@+id/bt_pop_wode" /> 

posted on 2016-08-19 16:59  巫山老妖  阅读(575)  评论(0编辑  收藏  举报