国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android 自定義TextView實現(xiàn)文本內(nèi)容自動調(diào)整字體大小

2019-10-23 18:33:45
字體:
供稿:網(wǎng)友

最近做通訊錄小屏機 聯(lián)系人姓名顯示--長度超過邊界字體變小

/**  * 自定義TextView,文本內(nèi)容自動調(diào)整字體大小以適應(yīng)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());   } } 

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持VEVB武林網(wǎng)!


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 大理市| 迁安市| 阿合奇县| 沭阳县| 永年县| 三台县| 清新县| 博野县| 澎湖县| 汤原县| 桂林市| 宁晋县| 车致| 博客| 甘谷县| 三亚市| 磐安县| 富平县| 临清市| 营口市| 北碚区| 密云县| 浦城县| 苍溪县| 江油市| 象山县| 桐柏县| 六枝特区| 普定县| 玉树县| 富源县| 沂南县| 三台县| 博爱县| 安图县| 宜州市| 金平| 朝阳区| 南郑县| 廉江市| 余干县|