无法单击PopupWindow的项目

时间:2016-04-01 06:10:24

标签: android android-popupwindow

我正在实施以下屏幕:

Screenshot

在这里你可以看到我有一个弹出窗口,其中包含不同的图标,如图库,照片等。

弹出窗口的布局是:

    <?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:background="@color/while_color"
    android:orientation="vertical"
    android:padding="@dimen/padding10">

    <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="Video" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/margin10"
        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>

我使用以下代码附加工具栏下的弹出窗口:

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


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


private void initializePopUpWindow() {
    //Placing the popup window below the toolbar
    popupWindow.showAsDropDown(toolbar, 0, 0);
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.popupwindow_attachment, null);
    TextView gallery = (TextView) v.findViewById(R.id.gallery);
    gallery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(), "Gallery Clicked", Toast.LENGTH_SHORT).show();
        }
    });
}

在这里,我试图在点击Gallery图标时干预消息,但它对我不起作用。请帮助访问弹出窗口的图标。

2 个答案:

答案 0 :(得分:3)

您正在initializePopUpWindow()方法中再次膨胀您的弹出式视图,并且您没有使用此视图,这就是您没有获得点击的原因。

声明您编写弹出窗口代码的点击事件代码。

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

        TextView gallery = (TextView)layout.findViewById(R.id.gallery);
        gallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "Gallery Clicked", Toast.LENGTH_SHORT).show();
            }
        });

答案 1 :(得分:2)

您可以使用弹出视图获取弹出窗口图标。

 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  popupWindow = new PopupWindow(layout, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);


 //you can access popup icon and click


TextView gallery = (TextView)layout.findViewById(R.id.gallery);


gallery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

//or

 layout.findViewById(R.id.gallery).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

//or 

 gallery.setOnClickListener(this);


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