2017-08-05 25 views
0

我的目标是创建一个对话框,其中包含其内容高度,如下图所示。包装内容无法用于对话框

enter image description here

我的问题是,我想要的对话框如下图所示的图像中,以匹配其母公司。高度应该停在“asdf”这个项目上,这个“asdf”下面的空白不应该存在。

enter image description here

这是我后来的代码:

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> 

我在做什么错?

+0

请张贴对话框的布局文件的代码。 –

回答

-1

你的问题是在你的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> 

说明: 问题是SwipeRefreshLayout。我评论它

+0

这一个正常工作。谢谢 – TSR

0

试试这个

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

这不起作用。我已经在这个问题中尝试过了。即使我添加windw.setGravity(Gravity.CENTER)它给出了相同的结果 – TSR

0

有两个选项供您

使用RecyclerView,而不是的ListView

或设置ADA后

使用下面的代码PTER。

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(); 
} 
+0

它不起作用。看到结果:[链接](https://scontent.fmru1-1.fna.fbcdn.net/v/t34.0-12/20667977_164129360813060_421894345_n.png?oh=7901ef5789be95ce9d069c822ff63c58&oe=5987E563) – TSR

+0

其实它适用于你。只是使其重力顶部 –