无法在android中处理弹出窗口

时间:2016-02-09 13:28:58

标签: android android-popupwindow

我正在尝试实现以下屏幕。在点击附件图标时,会显示弹出窗口:

jQuery API

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

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实现弹出窗口

   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);

    }

使用此代码后,我将收到以下屏幕:

Screenshot

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

已编辑的代码:

  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();

}

使用上面的代码后,弹出窗口位于我想要的工具栏下方,但在外面点击后它没有关闭。屏幕上没有任何功能正在工作。屏幕被严重绞死。

编辑工作代码:

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);
    }

现在它正在为我工​​作。

2 个答案:

答案 0 :(得分:3)

如果您希望弹出窗口位于顶部,则需要设置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

答案 1 :(得分:2)

你可以这样做:

@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);
}