无法处理 android 中的弹出窗口 Window

Unable to handle Popup Window in android

我正在尝试在单击附件图标时实现以下 screen.Here,弹出 window 显示:

我正在尝试使用下面提到的代码来实现屏幕:

1.popupwindow_attachment.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup_element"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="@dimen/padding10"
    android:background="@color/while_color">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/gallery"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawablePadding="@dimen/padding10"
            android:drawableTop="@drawable/gallery"
            android:gravity="center"
            android:text="Gallery" />

        <TextView
            android:id="@+id/photos"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawablePadding="@dimen/padding10"
            android:drawableTop="@drawable/photos"
            android:gravity="center"
            android:text="Photos" />

        <TextView
            android:id="@+id/videos"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawablePadding="@dimen/padding10"
            android:drawableTop="@drawable/videos"
            android:gravity="center"
            android:text="Gallery" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/audio"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawablePadding="@dimen/padding10"
            android:drawableTop="@drawable/audio"
            android:gravity="center"
            android:text="Audio" />

        <TextView
            android:id="@+id/location"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawablePadding="@dimen/padding10"
            android:drawableTop="@drawable/location"
            android:gravity="center"
            android:text="Location" />

        <TextView
            android:id="@+id/contacts"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawablePadding="@dimen/padding10"
            android:drawableTop="@drawable/contacts"
            android:gravity="center"
            android:text="Contacts" />
    </LinearLayout>

</LinearLayout>

2.Code 实现 Popup window

   private void initializePopUpWindow() {

        //inflate the popupwindow_attachment.xml
        LinearLayout viewGroup = (LinearLayout) SingleChatActivity.this.findViewById(R.id.popup_element);
        LayoutInflater inflater = (LayoutInflater) SingleChatActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.popupwindow_attachment, viewGroup);
        popupWindow = new PopupWindow(layout, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT,true);

        //Displaying the popup at a specific location
        popupWindow.showAtLocation(layout,Gravity.CENTER,0,0);

    }

使用此代码后出现以下屏幕:

如您所见,弹出窗口 window 不在工具栏下方。请帮我解决问题。

编辑代码:

  private void initializePopUpWindow() {

    //inflate the popupwindow_attachment.xml
    LinearLayout viewGroup = (LinearLayout) SingleChatActivity.this.findViewById(R.id.popup_element);
    LayoutInflater inflater = (LayoutInflater) SingleChatActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.popupwindow_attachment, viewGroup);
    popupWindow = new PopupWindow(layout, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true);

    //Displaying the popup at a specific location
   // popupWindow.showAtLocation(layout, Gravity.TOP, 0, 150);
    popupWindow.showAsDropDown(toolbar,0,0);

    //Close the popup when touch outside
    popupWindow.setOutsideTouchable(true);
    popupWindow.setFocusable(true);
   // popupWindow.dismiss();

}

使用上面的代码后,弹出窗口 window 在我想要的工具栏下方,但在单击外部后它没有关闭 it.No 功能在 screen.Screen 中被严重挂起.

已编辑工作代码:

1.onCreate()方法

 protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_single_chat);

            //Toolbar
            toolbar = (Toolbar) findViewById(R.id.toolbarSingleChat);
            toolbar.setNavigationIcon(R.drawable.back); // Setting Navigation Icon in the Toolbar
            setSupportActionBar(toolbar);

     LinearLayout viewGroup = (LinearLayout) SingleChatActivity.this.findViewById(R.id.popup_element);
            LayoutInflater inflater = (LayoutInflater) SingleChatActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.popupwindow_attachment, viewGroup);
            popupWindow = new PopupWindow(layout, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);

            //Close the popup when touch outside
            popupWindow.setOutsideTouchable(true);
            popupWindow.setFocusable(true);
            popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

2.onoptionsItemSelected()

 public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_viewContacts:
                return true;
            case R.id.action_media:
                return true;
            case R.id.action_search:
                return true;
            case R.id.action_block:
                return true;
            case R.id.action_email_chat:
                return true;
            case R.id.action_clear_chat:
                return true;
            case R.id.action_attach:
                initializePopUpWindow();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

3.initializePopUpWindow()方法:

 private void initializePopUpWindow() {
      popupWindow.showAsDropDown(toolbar, 0, 0);
    }

现在对我有用了。

如果您希望弹出窗口位于顶部,则需要设置 Gravity.TOP 而不是 Gravity.CENTER

替换

 popupWindow.showAtLocation(layout,Gravity.CENTER,0,0);

 popupWindow.showAtLocation(layout,Gravity.TOP,0,0);

更新

如果你想要工具栏下方的弹出窗口,那么你应该像

 popupWindow.showAtLocation(layout,Gravity.TOP,0,100);// where 100 is the height of toolbar

你可以这样做:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView)findViewById(R.id.text);
    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showpopUp();
        }
    });

}

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

 popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    popupWindow.setOutsideTouchable(true);
    popupWindow.setFocusable(true);

    popupWindow.showAsDropDown(textView, 0, 0);
}