本文起因:UI給的標注圖上牺氨,兩個垂直TextView之間狡耻,標注了xxdp。標注符號在上一行文字底部猴凹,緊貼文字夷狰,以及下一行文字頂部,也是緊貼文字郊霎。
一兩行問題不大沼头,可是某些情況下可能會有七八行甚至十來行,完书劝,出事了进倍,發(fā)現(xiàn)高度不夠。
截圖細查购对,發(fā)現(xiàn)是TextView設(shè)置的MarginTop按圖上標的xxdp不行猾昆,因為MarginTop是按照TextView的外框來算的,文字到外框會有點點白邊骡苞。
這個白邊上下也就1dp左右垂蜗,一兩行不怎么看得出楷扬,十來行放一起,完么抗,這就20dp毅否,這就偏得有點遠了。
下面蝇刀,我們通過自定義一個TextView來解決這個問題:
首先幾個概念需要先知道螟加,大家知道的一起復習一下哈,不知道的隨便看看留個印象吞琐。
儲備知識:FontMetrics
其中存在:top,ascent,descent,bottom,leading這四個量捆探。
首先:FontMetrics這個類的作用是用于文字測量。
接著:一個文字可以劃分為這么幾個部分:
其中:
Baseline:
文本繪制的起始點站粟,可以視為0點黍图,其余參數(shù)都是以它為準進行計算的。需要注意的是:向上為負數(shù)奴烙,向下為正數(shù)
Top:
文本的最高點助被,注意,這里包括了頂部的所有注音符號切诀。什么是注音符號揩环?漢語拼音您還記得不?對幅虑,那上面的符號就是注音符號丰滑。
注意,這個數(shù)值一般為負值倒庵。
Ascent:
從Baseline開始褒墨,至注音符號之前,也就是不含有注音符號的高度擎宝,一般稱為上坡度郁妈。
注意,這個數(shù)值一般為負值认臊。
Bottom:
文本的最低點圃庭,注意,這里也包括了底部的所有注音符號失晴,有某些國家的注音符號是在底部標注的剧腻。
注意,這個數(shù)值一般為正值涂屁。
Descent:
從Baseline開始书在,到底部注音符號之前,與Ascent正好相反拆又,也是不含有注音符號的高度儒旬,一般稱為下坡度栏账。
注意,這個數(shù)值一般為正值栈源。
Leading:
表示上一行文本從Descent至本行文本Ascent的距離挡爵。一般稱為行間距。
OK甚垦,補充了如上知識后茶鹃,可以瞬間得出,我們輸入的文本艰亮,幾乎不帶有注音符號闭翩,因此可以考慮移除Math.abs(top) - Math.abs(ascent)的距離,以及Math.abs(bottom) - Math.abs(descent)的距離迄埃。
好的疗韵,我們上源碼:
package com.xxx.xxx.ui.view;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Paint.FontMetricsInt;
import android.os.Build.VERSION_CODES;
import android.util.AttributeSet;
import android.widget.TextView;public class CustomTextView extends TextView {
? ? ? public CustomTextView(Context context) {
? ? ? ? ? ? super(context);
? ? ? }? ? ? public CustomTextView(Context context, AttributeSet attrs) {
? ? ? ? ? ? super(context, attrs);
? ? ? }? ? ? public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
? ? ? ? ? ? super(context, attrs, defStyleAttr);
? ? ? }
? ? ? @TargetApi(VERSION_CODES.LOLLIPOP)? ? ? public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
? ? ? ? ? ? super(context, attrs, defStyleAttr, defStyleRes);
? ? ? }? ? ? private FontMetricsInt fontMetricsInt;
? ? ? @Override
? ? ? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
? ? ? ? ? ? super.onMeasure(widthMeasureSpec, heightMeasureSpec);
? ? ? ? ? ? int heightAndState = getMeasuredHeightAndState();
? ? ? ? ? ? int widthAndState = getMeasuredWidthAndState();
? ? ? ? ? ? int heightModeOrigin = MeasureSpec.getMode(heightMeasureSpec);
? ? ? ? ? ? int heightSizeOrigin = MeasureSpec.getSize(heightAndState);
? ? ? ? ? ? int heightMode = MeasureSpec.getMode(heightAndState);
? ? ? ? ? ? if (fontMetricsInt == null) {
? ? ? ? ? ? ? ? ? fontMetricsInt = new FontMetricsInt();
? ? ? ? ? ? ? ? ? getPaint().getFontMetricsInt(fontMetricsInt);
? ? ? ? ? ? }
? ? ? ? ? ? int top = Math.abs(fontMetricsInt.top);
? ? ? ? ? ? int ascent = Math.abs(fontMetricsInt.ascent);
? ? ? ? ? ? int bottom = Math.abs(fontMetricsInt.bottom);
? ? ? ? ? ? int descent = Math.abs(fontMetricsInt.descent);
? ? ? ? ? ? int heightSize = heightSizeOrigin - (top - ascent) - (bottom - descent);
? ? ? ? ? ? if (heightModeOrigin == MeasureSpec.AT_MOST) {
? ? ? ? ? ? ? ? ? int newHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode);
? ? ? ? ? ? ? ? ? setMeasuredDimension(widthAndState, newHeightMeasureSpec);
? ? ? ? ? ? }
? ? ? }
}
源碼很簡單,但是侄非,簡書居然沒有貼源碼的功能蕉汪,真不專業(yè)……果然還是CSDN靠譜么……