Inflating a pop up window / parent viewgroup

2019-03-04 07:56发布

问题:

I have a pop up window that inflates a list view in a relative layout and a list view that inflates a text view in a relative layout. The width of the pop up window defaults to match parent eventhough I have defined everywhere wrap content, both in layout files and programmatically, only.

I have inflated the list view in a relative layout in a pop up window like this.

view1 = inflater.inflate(resource1, null);

and I have inflated the text view in a relative layout in a list view like this.

(Some lines that are not specific enough to this asked question have been removed).

@Override
public View getView(int position, View view, ViewGroup viewgroup) {
    ...
    view1 = inflater.inflate(resource1, null);
    ...
}

The pop up window displays something like below image.

If I change view1 = inflater.inflate(resource1, null) to view1 = inflater.inflate(resource1, viewgroup, false) then the display changes to something like this.

However, if I change

PopupWindow popupwindow_obj = popupDisplay();
popupwindow_obj.showAsDropDown(v, 0, 0); // where u want show on view click event popupwindow.showAsDropDown(view, x, y);

public PopupWindow popupDisplay()
{
    final PopupWindow popupWindow = new PopupWindow(this);
    LayoutInflater inflater = (LayoutInflater) (Home.this).getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View view2 = inflater.inflate(R.layout.activity_list, null);
    ListView listView1 = (ListView) view2.findViewById(R.id.listView3);

    custom_adapter custom = new custom_adapter(Home.this, R.layout.activity_message, information);
    listView1.setOnItemClickListener(onitemclick);
    listView1.setAdapter(custom);

    popupWindow.setFocusable(true);
    popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
    popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    popupWindow.setContentView(view2);

    return popupWindow;
}

to

PopupWindow popupwindow_obj = popupDisplay();
popupwindow_obj.showAsDropDown(v, 0, 0); // where u want show on view click event popupwindow.showAsDropDown(view, x, y);

public PopupWindow popupDisplay()
{
    LayoutInflater inflater = (LayoutInflater) (Home.this).getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View view2 = inflater.inflate(R.layout.activity_list, null);
    ListView listView1 = (ListView) view2.findViewById(R.id.listView3);

    final PopupWindow popupWindow = new PopupWindow(view2, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

    custom_adapter custom = new custom_adapter(Home.this, R.layout.activity_message, information);
    listView1.setOnItemClickListener(onitemclick);
    listView1.setAdapter(custom);

    popupWindow.setFocusable(true);
    popupWindow.setContentView(view2);

    return popupWindow;
}

then the display changes like this, with the transparent portion of the pop up window being out of action, entirely.

  • I am looking to have a width of pop up window to wrap the content of the text views inside the list view by taking the width of the largest text view and then align all text to the left, either in bold typeface with white background (unread messages) and normal typeface in grey background (read messages). Please help me out.

  • Is there any parent view group that can be given to view1 = inflater.inflate(resource1, null)? I am not sure about it. It seems that it is this view that should be inflated to a root view. Inflating a root view to view1 = inflater.inflate(resource1, null) seems to result in different widths of text views inside of list view, the remaining portion being transparent.

  • Making use of view1 = inflater.inflate(resource1, viewgroup, true) results in the following error message. Why? Is there any way that anyone over here by knows that it can be fixed up with in?

java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView.

Thank you in advance.

See also related question over here by, too.