2016-09-17 3 views
1

실제로 최종 높이를 계산하는 방법은 textView입니다.실제로 TextView 높이를 측정하는 방법은 무엇입니까?

TextView의 높이 측정에 다음 방법을 시도했습니다.

public static int getHeight(Context context, String text, int textSize, int deviceWidth) { 
     EmojiTextView textView = new EmojiTextView(context); 
     textView.setText(text); 
     textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize); 
     int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(deviceWidth, View.MeasureSpec.AT_MOST); 
     int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); 
     textView.measure(widthMeasureSpec, heightMeasureSpec); 
     float lineSpacingMulti = textView.getLineSpacingMultiplier(); 
     float lineSpacingExtra = textView.getLineSpacingExtra(); 
     float lineHeight = textView.getLineHeight(); 
     Log.d(TAG, "getHeight: lineSpacingMulti" + lineSpacingMulti + ":lineSpacingExtra:" + lineSpacingExtra + ":lineHeight:" + lineHeight); 
     return textView.getMeasuredHeight(); 
    } 

나는 다음있어 로그 캣 결과 :

09-17 12:11:18.974 25729-25729/?: getHeight: lineSpacingMulti1.0:lineSpacingExtra:0.0:lineHeight:43.0 

09-17 12:11:18.973 25729-25729/?: calculateDynamicTextSize: ME Height 58 

라인의 높이가 43와 승수는 측정 텍스트의 높이가 58 이유 1보다입니다 간격 라인입니다 그렇다면?

+0

을 알려주세요. 실제로 어떻게해야할까요? – pskink

+0

@pskink 텍스트의 위쪽 및 아래쪽 패딩을 측정하고 있습니다. 그래서 두 개의 textView를 LinearLayout이나 하나의 textView와 하나의 ImageView에 넣으면 그 둘 사이의 공간은 똑같아 야합니다. – Kaushlendra

+0

'TextView # setIncludeFontPadding'을 참조하십시오.하지만'LinearLayout'에서 두 개 이상을 병합하는 대신'TextView'를 사용하지 않으시겠습니까? – pskink

답변

0

뷰를 렌더링 할 때 창의 위치와 높이가 계산됩니다. 추가 선을 하나 더 추가하더라도 58로 유지됩니다. 라인 높이는 페인트 된 텍스트의 높이에 해당하는 반면 textviews 높이는 렌더 된 뷰의 높이에 해당합니다. 그것을 가지고 놀자 : dp와 같이 textview의 높이를 30으로 적용한 다음, 내용을 감싸고 부모와 일치하고 logcat을 봅니다. `TextView # onMeasure` 메쏘드에 대해 정말로 알고 싶다면

+0

TextSize 32에 대한 결과는 다음과 같습니다. 09-17 13 : 12 : 12.611 25729-25729/?: getHeight() : text = [Me], textSize = [32], deviceWidth = [1562] 09 -17 13 : 12 : 12.613 25729-25729/?: getHeight : lineSpacingMulti1.0 : lineSpacingExtra : 0.0 : lineHeight : 99.0 : 선 경계 : Rect (0, 0 - 118, 132) 09-17 13 : 12 : 12.614 25729 -25729/?: calculateDynamicTextSize : ME 높이 132 – Kaushlendra

관련 문제