3

我有这个代码:

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 的范围之外单击时,它不能被取消。

4

1 回答 1

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>

如果有任何疑问,请在评论中问我。为自己是安卓人而自豪:)

于 2015-01-06T12:47:04.627 回答