您好,登錄后才能下訂單哦!
這篇文章給大家介紹怎么在Android中利用TextView調(diào)整文本內(nèi)容的字體大小,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
具體方法如下:
/** * 自定義TextView,文本內(nèi)容自動調(diào)整字體大小以適應TextView的大小 * @author yzp */ public class AutoFitTextView extends TextView { private Paint mTextPaint; private float mTextSize; public AutoFitTextView(Context context) { super(context); } public AutoFitTextView(Context context, AttributeSet attrs) { super(context, attrs); } /** * Re size the font so the specified text fits in the text box assuming the * text box is the specified width. * * @param text * @param textWidth */ private void refitText(String text, int textViewWidth) { if (text == null || textViewWidth <= 0) return; mTextPaint = new Paint(); mTextPaint.set(this.getPaint()); int availableTextViewWidth = getWidth() - getPaddingLeft() - getPaddingRight(); float[] charsWidthArr = new float[text.length()]; Rect boundsRect = new Rect(); mTextPaint.getTextBounds(text, 0, text.length(), boundsRect); int textWidth = boundsRect.width(); mTextSize = getTextSize(); while (textWidth > availableTextViewWidth) { mTextSize -= 1; mTextPaint.setTextSize(mTextSize); textWidth = mTextPaint.getTextWidths(text, charsWidthArr); } this.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); refitText(this.getText().toString(), this.getWidth()); } }
關于怎么在Android中利用TextView調(diào)整文本內(nèi)容的字體大小就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。