Set ListView Height Programmatically

前端 未结 3 882
小鲜肉
小鲜肉 2021-01-12 17:01

I have a TextView and ListView in my activity.ListView intially set to Visibility = gone I need a functionality like if u

3条回答
  •  佛祖请我去吃肉
    2021-01-12 17:50

    This will help you.

     ListAdapter listadp = listview.getAdapter();
           if (listadp != null) {
               int totalHeight = 0;
               for (int i = 0; i < listadp.getCount(); i++) {
                   View listItem = listadp.getView(i, null, listview);
                   listItem.measure(0, 0);
                   totalHeight += listItem.getMeasuredHeight();
               }
               ViewGroup.LayoutParams params = listview.getLayoutParams();
               params.height = totalHeight + (listview.getDividerHeight() * (listadp.getCount() - 1));
               listview.setLayoutParams(params);
               listview.requestLayout();
    

提交回复
热议问题