Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 包装不适用于对话框的内容_Java_Android_Listview_Dialog - Fatal编程技术网

Java 包装不适用于对话框的内容

Java 包装不适用于对话框的内容,java,android,listview,dialog,Java,Android,Listview,Dialog,我的目标是创建一个包装其内容高度的对话框,如下图所示 我的问题是我的对话框想要匹配它的父对象,如下图所示。高度应在项目“asdf”处停止,且“asdf”下方的空白不应存在 这是我以后的代码: private void showPopupEventTest(final String Cardid) { Dialog dialog = new Dialog(getActivity(), R.style.Theme_AppCompat_Dialog_Alert);

我的目标是创建一个包装其内容高度的对话框,如下图所示

我的问题是我的对话框想要匹配它的父对象,如下图所示。高度应在项目“asdf”处停止,且“asdf”下方的空白不应存在

这是我以后的代码:

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

    }
这是布局图:

<?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>

我做错了什么?

试试这个

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

你有两个选择

使用RecyclerView而不是ListView

设置适配器后使用以下代码

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();
}  
public static void setListViewHeightBasedOnChildren(ListView ListView){
ListAdapter ListAdapter=listView.getAdapter();
如果(listAdapter==null){
//前提条件
返回;
}
int totalHeight=0;
对于(int i=0;i
您的问题在于xml布局。试试这个:

<?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>

说明:
问题是布局的问题。我对此发表了评论

这不起作用。我已经试过这个问题了。即使我加上windw.setGravity(Gravity.CENTER),它也会给出同样的结果,但不起作用。看看结果:实际上它对你有用。只需将其重力置于顶部,然后发布对话框布局文件的代码。