在PopupWindow的边界外单击时无法取消

时间:2012-12-07 09:25:44

标签: android popupwindow

我有这段代码:

PopupWindow  myMenu = new PopupWindow(menuView, LayoutParams.FILL_PARENT,            LayoutParams.FILL_PARENT,true);  
myMenu.setOutsideTouchable(true);  
myMenu.setBackgroundDrawable(new BitmapDrawable());
myMenu.setTouchable(true);   
myMenu.setFocusable(true); 
myMenu.update();  

如何定义PopupWindow?弹出PopupWindow时,单击后退按钮时可以取消它。当我在PopupWindow的边界外点击时,它无法取消。

1 个答案:

答案 0 :(得分:0)

  

Popup是一个窗口控件。它位于android.widget.popupWindow中。   我们可以像这样使用

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button btnOpenPopup = (Button)findViewById(R.id.openpopup);
    btnOpenPopup.setOnClickListener(new Button.OnClickListener(){



@Override
   public void onClick(View arg0) {
    LayoutInflater layoutInflater 
     = (LayoutInflater)getBaseContext()
      .getSystemService(LAYOUT_INFLATER_SERVICE);  
    View popupView = layoutInflater.inflate(R.layout.popup, null);  
             final PopupWindow popupWindow = new PopupWindow(
               popupView, 
               LayoutParams.WRAP_CONTENT,  
                     LayoutParams.WRAP_CONTENT

);



         Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
         btnDismiss.setOnClickListener(new Button.OnClickListener(){

 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  popupWindow.dismiss();
 }});

         popupWindow.showAsDropDown(btnOpenPopup, 50, -30);
          alertDialog.setCanceledOnTouchOutside(false);


 }});
    }
  

这是popup.xml中popupWindow的布局

<?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:orientation="vertical" 
    android:background="@android:color/background_light">
     <LinearLayout 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical" 
      android:layout_margin="20dp">
      <TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="It's a PopupWindow" />
      <Button
          android:id="@+id/dismiss"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="Dismiss" />
      </LinearLayout>
 </LinearLayout>
  

如果有任何疑问,请在评论中提问我。 Proude成为一名安卓人员:)