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

前端 未结 3 1519
北海茫月
北海茫月 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条回答
  • 2020-12-15 11:47

    It also gives wrong value when Your xml file of listview item has padding. Remove padding, try using margin and it will work perfectly.

    0 讨论(0)
  • 2020-12-15 11:53

    This is the only solution i have found so far. http://syedrakibalhasan.blogspot.com/2011/02/how-to-get-width-and-height-dimensions.html

    0 讨论(0)
  • 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));
    
    0 讨论(0)
提交回复
热议问题