2017-11-08 18 views
0

私は外にタップするとPopupWindowを却下するにはどうすればいいですか?ポップアップウィンドウを外す方法

これは私のコードです:

private void initiatePopupWindow(View viewButton) { 
     LayoutInflater inflater = (LayoutInflater) MainActivity.this 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view = inflater.inflate(R.layout.popup_layout, null); 
     mPopupWindow = new PopupWindow(this); 
     mPopupWindow.setContentView(view); 
     mPopupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT); 
     mPopupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); 
     mPopupWindow.showAsDropDown(viewButton); 
//  final PopupWindow pw = new PopupWindow(inflater.inflate(
//    R.layout.popup_layout, null, false), LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, true); 
//  pw.showAtLocation(view, Gravity.BOTTOM, 0, view.getHeight()); 
     ListView moreOptions = (ListView) mPopupWindow.getContentView().findViewById(R.id.list_view); 
     String[] options = new String[]{"Settings", "Feedback", "Contribute", "DashClock Integration", "Tutorial", "DashClock Integration", "Tutorial", "DashClock Integration", "Tutorial", "DashClock Integration", "Tutorial", "DashClock Integration", "Tutorial"}; 

     final ArrayList<String> moreOptionsList = new ArrayList<String>(); 
     for (int i = 0; i < options.length; ++i) { 
      moreOptionsList.add(options[i]); 
     } 
     ArrayAdapter<String> moreOptionsAdapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, 
       moreOptionsList); 
     moreOptions.setAdapter(moreOptionsAdapter); 
//  mPopupWindow.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(this, android.R.color.transparent))); 
     mPopupWindow.setFocusable(true); 
     mPopupWindow.setBackgroundDrawable(new ColorDrawable()); 
     mPopupWindow.setOutsideTouchable(true); 
//  mPopupWindow.setTouchInterceptor(new View.OnTouchListener() { 
//   @Override 
//   public boolean onTouch(View v, MotionEvent event) { 
//    if (event.getAction() == MotionEvent.ACTION_OUTSIDE){ 
//     mPopupWindow.dismiss(); 
//    } 
//    return false; 
//   } 
//  }); 
    } 

私は、次のとおりです。

mPopupWindow.setFocusable(true); 
mPopupWindow.setBackgroundDrawable(new ColorDrawable()); 
mPopupWindow.setOutsideTouchable(true); 

しかし、それは働いていません。これをどうやって解決するのですか?

+0

これを解決するには、mPopupWindow.setBackgroundDrawable(新しいBitmapDrawable(getResources()、 "")))の下にshowAsDropDown(viewButton)を配置します。 mPopupWindow.setOutsideTouchable(true); – JackieChung

答えて

0

これは私のコードでの作業で、これを試してみてください。

mPopupWindow.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(this, android.R.color.transparent))); 
mPopupWindow.setOutsideTouchable(true); 
コーディング

ハッピー!!

+0

それは私のために働かない:(アンドロイド5.1 – JackieChung

関連する問題