ViewGroup{TextView,…}.getMeasuredHeight gives wrong value is smaller than real height

前端 未结 3 1632
北海茫月
北海茫月 2020-12-15 11:14
  { January 14, 2011... I have given up to use setListViewHeightBasedOnChildren(ListView listView},
  instead, I don\'t put my listview in a scrollview, and then jus         


        
3条回答
  •  旧时难觅i
    2020-12-15 12:04

    The question is rather old, but I had similar problem, so I'll describe what was wrong. Actually, parameters in listItem.measure() are used wrong, you should set something like this:

    listItem.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED))
    

    However, be careful with unspecified width measure spec, it will ignore all layout params and even screen dimensions, so to get correct height, first get maximum width View can use and call measure() this way:

    listItem.measure(MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    

提交回复
热议问题