2014-12-27 387 views
0

我在Android中彈出窗口時遇到了一些問題。我想要做的是當用戶第一次點擊按鈕時,彈出窗口會顯示。如果彈出窗口顯示並且用戶再次點擊該按鈕,彈出窗口將被隱藏。下面是我的XML的彈出窗口:Android彈出窗口在彈出窗口外彈出時解僱

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/llAttendeeList" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:background="#000000" 
> 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="HELLO" /> 

</LinearLayout> 

與行爲守則,其中上點擊按鈕,就會執行這個方法:

private void openPopUp(){ 
    LayoutInflater layoutInflater = (LayoutInflater) getActivity() 
      .getBaseContext().getSystemService(
        context.LAYOUT_INFLATER_SERVICE); 
    View popupView = layoutInflater.inflate(
      R.layout.event_attendee_pop, null); 
    llAttendeeList = (LinearLayout) popupView 
      .findViewById(R.id.llAttendeeList); 
    final PopupWindow popupWindow = new PopupWindow(popupView, 
      LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
    popupWindow.setOutsideTouchable(true); 

    popupWindow.setTouchInterceptor(new OnTouchListener() { 
       public boolean onTouch(View v, MotionEvent event) { 
        if (event.getAction() == MotionEvent.ACTION_OUTSIDE) 
        { 
         popupWindow.dismiss(); 
         return true; 
        } 
        return false; 
       } 
      }); 
    popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0); 
} 

但是,當用戶單擊這個按鈕,在彈出的做出現了。但是當我在彈出窗口外單擊時,它不會消失。有任何想法嗎?

在此先感謝。

回答

-2

添加popupWindow.setContentView(popupView),然後再試一次

+0

你有什麼想法如何設置彈出的正下方有操作欄marginTop? –

+0

有什麼想法?我無法將其設置到我想要的位置 –

1

我想通了,我已經設置這個彈出窗口:

popupWindow.setOutsideTouchable(true); 
    popupWindow.setBackgroundDrawable(new ColorDrawable());