Limit height of ListView on Android

后端 未结 16 1342
广开言路
广开言路 2020-11-28 23:07

I want to show a button under a ListView. Problem is, if the ListView gets extended (items added...), the button is pushed out of the screen.

16条回答
  •  一向
    一向 (楼主)
    2020-11-28 23:41

    I solved this problem in code, setting height of listview only if it has more than 5 items in it:

    if(adapter.getCount() > 5){
            View item = adapter.getView(0, null, listView);
            item.measure(0, 0);         
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, (int) (5.5 * item.getMeasuredHeight()));
            listView.setLayoutParams(params);
    }
    

    Notice that I set the max height to 5.5 times the height of a single item, so the user will know for sure there is some scrolling to do! :)

提交回复
热议问题