這裏用這種方法可以識別的TextView高度
public static int getHeight(Context context, String text, int textSize, int deviceWidth) {
TextView textView = new TextView(context);
textView.setText(text);
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(deviceWidth, MeasureSpec.AT_MOST);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
textView.measure(widthMeasureSpec, heightMeasureSpec);
return textView.getMeasuredHeight();
}
如果textSize
不像素給出,改變setTextSize()
第一個參數中。
從How to Measure TextView Height Based on Device Width and Font Size?
你檢查了這個:https://stackoverflow.com/questions/3779173/determining-the-size-of-an-android-view-at-runtime? – fiddler