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

时间:2016-06-24 04:16:59

标签: android listview android-popupwindow

我把listView放在popupWindow中,当它有很多项目时,popupWindow超出了屏幕大小,所以我想限制listView的最大高度(或者最大项目,如果超过显示滚动条),谢谢所有< / p>

3 个答案:

答案 0 :(得分:0)

试试这可能会对你有所帮助

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

}

你可以像这样使用

     YourAdapter mYoursAdapter = new YourAdapter();
     mListview.setAdapter(mYoursAdapter);
     setListViewHeightBasedOnChildren(mListview);

答案 1 :(得分:0)

在项目中创建一个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);
          }
    }

答案 2 :(得分:0)

我认为你可以在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不会像以前一样覆盖整个屏幕。