调用StaticLayout的getHeight()返回值与animView实际高度不符,求原因
Got it, let's figure out why your StaticLayout's height doesn't match your animView's actual height—this is a common pitfall with text layout on Android, and there are a few key reasons this happens:
1. You're ignoring the View's padding
When you create the StaticLayout with animView.getMeasuredWidth(), you're using the full measured width of the View, but the text inside the View doesn't actually use that entire space—horizontal padding cuts into the available width. So your StaticLayout is laying out text for a wider area than what's actually rendered in the View, which can lead to fewer lines (and a shorter height) than the real thing. On top of that, the View's getHeight() includes top and bottom padding, while StaticLayout.getHeight() only counts the text itself.
Fix this by calculating the actual available width for text:
int textWidth = animView.getMeasuredWidth() - animView.getPaddingLeft() - animView.getPaddingRight(); StaticLayout staticLayout = new StaticLayout( animView.getText(), animView.getPaint(), textWidth, Layout.Alignment.ALIGN_NORMAL, animView.getLineSpacingMultiplier(), animView.getLineSpacingExtra(), animView.isIncludeFontPadding() // Match the View's font padding setting! );
2. Font padding settings don't match
That last true in your StaticLayout constructor controls whether extra padding is added for ascenders/descenders (like the top of a "b" or bottom of a "g"). If your animView has setIncludeFontPadding(false) enabled, but you're passing true to StaticLayout, the StaticLayout will be taller than the View's actual text height. Always sync this setting with the View using animView.isIncludeFontPadding().
3. Your View is truncating text with ellipsize
If animView uses setEllipsize(...) (like adding "..." to long text) and the text is actually getting truncated, the View's height is based on the shortened text. But your current StaticLayout uses the full, untruncated text, so its height will be way taller than the View's.
To fix this, use the StaticLayout.Builder (available from API 23+) which supports ellipsize:
int textWidth = animView.getMeasuredWidth() - animView.getPaddingLeft() - animView.getPaddingRight(); StaticLayout staticLayout = StaticLayout.Builder.obtain( animView.getText(), 0, animView.getText().length(), animView.getPaint(), textWidth ) .setAlignment(Layout.Alignment.ALIGN_NORMAL) .setLineSpacing(animView.getLineSpacingExtra(), animView.getLineSpacingMultiplier()) .setIncludePad(animView.isIncludeFontPadding()) .setEllipsize(animView.getEllipsize()) // Match the View's ellipsize mode .setEllipsizedWidth(textWidth) .build();
4. Rare case: The View hasn't finished laying out yet
While post(Runnable) usually ensures the View is measured, in complex layouts, sometimes the View hasn't completed its layout pass when the Runnable runs. Try swapping animView.getHeight() with animView.getMeasuredHeight()—measuredHeight is set during the measurement phase, while getHeight() comes after layout. This is a long shot, but worth checking if the other fixes don't work.
内容的提问来源于stack exchange,提问作者ZSAchg

