Wrap content not working for dialog

时间:2017-08-05 11:10:48

标签: java android listview dialog

My goal is to create a dialog that wraps its content height as shown in the image below.

enter image description here

My problem is that my dialog wants to match its parent as shown in the image below. The height should stop at the item "asdf" and this white space below "asdf" should not exist.

enter image description here

This is my code for the later:

private void showPopupEventTest(final String Cardid) {
        Dialog dialog = new Dialog(getActivity(), R.style.Theme_AppCompat_Dialog_Alert);
        dialog.getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(R.layout.fragment_event);
        SwipeRefreshLayout swipeRefreshLt = (SwipeRefreshLayout) dialog.findViewById(R.id.swipeRefreshLt);
        ListView lv = (ListView) dialog.findViewById(R.id.Contacts_list_view);
        SearchView SearchET = (SearchView) dialog.findViewById(R.id.SearchET);
        LinearLayout layoutLinearLayout = (LinearLayout) dialog.findViewById(R.id.layoutLinearLayout);
        EventAdaptor myAdapter = new EventAdaptor(getContext(), ArrayList_EventObject);
        lv.setAdapter(myAdapter);
        dialog.getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        dialog.show();

    }

And this is the layout:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/layoutLinearLayout"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:paddingTop="8dp">

    <SearchView
        android:id="@+id/SearchET"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:iconifiedByDefault="false" />

    <android.support.v4.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:id="@+id/swipeRefreshLt"
        android:layout_height="wrap_content">

        <ListView
            android:id="@+id/Contacts_list_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

What am I doing wrong?

3 个答案:

答案 0 :(得分:0)

try this

Window window = customDialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
customDialog.show();

答案 1 :(得分:0)

There are two options for you

Use RecyclerView instead of ListView

OR

use below code after setting the adapter.

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter(); 
    if (listAdapter == null) {
        // pre - condition
        return;
    }

    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}  

答案 2 :(得分:-1)

Your problem is in your xml layout. Try this:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/layoutLinearLayout"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:paddingTop="8dp">

    <SearchView
        android:id="@+id/SearchET"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:iconifiedByDefault="false" />

    <!--<android.support.v4.widget.SwipeRefreshLayout-->
        <!--android:layout_width="match_parent"-->
        <!--android:id="@+id/swipeRefreshLt"-->
        <!--android:layout_height="wrap_content">-->

        <ListView
            android:id="@+id/Contacts_list_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    <!--</android.support.v4.widget.SwipeRefreshLayout>-->

</LinearLayout>

Explanation: The problem was the SwipeRefreshLayout. I commented it