这是我的函数计算器ListView的高度:
fun setListViewHeightBasedOnChildren(listView: ListView) {
val listAdapter = listView.getAdapter() ?: return
val desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED)
var totalHeight = 0
var view: View? = null
for (i in 0 until listAdapter.getCount()) {
view = listAdapter.getView(i, view, listView)
if (i == 0)
view!!.layoutParams = ViewGroup.LayoutParams(desiredWidth, LinearLayout.LayoutParams.WRAP_CONTENT)
view!!.measure(desiredWidth, MeasureSpec.UNSPECIFIED)
totalHeight += view.measuredHeight
}
val params = listView.getLayoutParams()
params.height = totalHeight + listView.getDividerHeight() * (listAdapter.getCount() - 1)
listView.setLayoutParams(params)
}
并致电显示所有项目:
setListViewHeightBasedOnChildren(listView)
为什么ListView的高度不正确?
答案 0 :(得分:1)
检查以下答案:
NSMutableDictionary
可能是你忘记了总的顶部和底部填充,并将params设置为listview项目,如上面的代码。