如題
在論壇裡面也回復你了
@Override
protected void onDraw(Canvas canvas) {
Drawable[] drawables = getCompoundDrawables();
if (drawables != null) {
Drawable drawableTop = drawables[1];
if (drawableTop != null) {
float textHeight=measureHeight(getText().toString());
int drawablePadding = getCompoundDrawablePadding();
int drawableHeight = drawableTop.getIntrinsicHeight();
float bodyHeight = textHeight + drawableHeight + drawablePadding;
setPadding(0, (int)(getHeight() - bodyHeight), 0, 0);
canvas.translate(0, 0-(getHeight() - bodyHeight) / 2);
}
}
super.onDraw(canvas);
}
//獲取文本高度
public int measureHeight(String text) {
// Rect result = new Rect();
// // Measure the text rectangle to get the height
// getPaint().getTextBounds(text, 0, text.length(), result);
// return result.height();
Paint.FontMetrics fontMetrics= getPaint().getFontMetrics();
return (int)(fontMetrics.descent-fontMetrics.ascent+ fontMetrics.leading);
}