Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.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
如何在android中设置listView的最大高度_Android_Listview_Android Popupwindow - Fatal编程技术网

如何在android中设置listView的最大高度

如何在android中设置listView的最大高度,android,listview,android-popupwindow,Android,Listview,Android Popupwindow,我将listView放在popupWindow中,当它有许多项目时,popupWindow超过屏幕大小,因此我想限制listView的最大高度(或最大项目,如果超过,则显示滚动条),谢谢大家 public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null)

我将listView放在popupWindow中,当它有许多项目时,popupWindow超过屏幕大小,因此我想限制listView的最大高度(或最大项目,如果超过,则显示滚动条),谢谢大家

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

         return;
   }


int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
for (int i = 0; i < 5; i++) //here you can set 5 row at a time if row excceded the scroll automatically available
{
    View listItem = listAdapter.getView(i, null, listView);
    if (listItem instanceof ViewGroup) {
         listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    }
      listItem.measure(0, 0);
      totalHeight += listItem.getMeasuredHeight();
}

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

}

试试这个,也许对你有帮助

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

         return;
   }


int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
for (int i = 0; i < 5; i++) //here you can set 5 row at a time if row excceded the scroll automatically available
{
    View listItem = listAdapter.getView(i, null, listView);
    if (listItem instanceof ViewGroup) {
         listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    }
      listItem.measure(0, 0);
      totalHeight += listItem.getMeasuredHeight();
}

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

}

在项目中创建一个类AppUtil。尝试如下操作:

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

        int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            if (listItem instanceof ViewGroup) {
                listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            }
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

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

}
    public class MedicalReportsFragment extends Fragment {
@Bind(R.id.lv_investigative_report)
    ListView investigativeReportLV;

      private MedicalReportsAdapter mMedicalReportsAdapter;
    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mMedicalReportsAdapter = new MedicalReportsAdapter(getActivity(), mMedicalReportOverviews,true);
    investigativeReportLV.setAdapter(mMedicalReportsAdapter);
            AppUtil.setListViewHeightBasedOnChildren(investigativeReportLV);
          }
    }

在项目中创建一个类AppUtil。尝试如下操作:

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

        int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            if (listItem instanceof ViewGroup) {
                listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            }
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

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

}
    public class MedicalReportsFragment extends Fragment {
@Bind(R.id.lv_investigative_report)
    ListView investigativeReportLV;

      private MedicalReportsAdapter mMedicalReportsAdapter;
    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mMedicalReportsAdapter = new MedicalReportsAdapter(getActivity(), mMedicalReportOverviews,true);
    investigativeReportLV.setAdapter(mMedicalReportsAdapter);
            AppUtil.setListViewHeightBasedOnChildren(investigativeReportLV);
          }
    }

我认为您可以在layout.xml中尝试以下技巧:

<ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="40dp" />

<LinearLayout
    android:id="@+id/layout_bottom_button"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_marginTop="-40dp" />


事实上,marginBottom将为ListView下面的布局保留40dp,因此ListView不会像以前那样覆盖整个屏幕。

我认为您可以在layout.xml中尝试以下技巧:

<ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="40dp" />

<LinearLayout
    android:id="@+id/layout_bottom_button"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_marginTop="-40dp" />


事实上,marginBottom将为ListView下面的布局保留40dp,因此您的ListView不会像以前那样覆盖整个屏幕。

检查此答案:谢谢,您的解决方案是有效的检查此答案:谢谢,您的解决方案是有效的