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

首頁 > 系統 > Android > 正文

Android 自定義TextView實現文本內容自動調整字體大小

2019-12-12 03:17:32
字體:
來源:轉載
供稿:網友

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

/**  * 自定義TextView,文本內容自動調整字體大小以適應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());   } } 

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 堆龙德庆县| 桑日县| 石首市| 桦甸市| 陵川县| 文昌市| 大余县| 独山县| 红桥区| 调兵山市| 邵东县| 株洲县| 宜阳县| 思茅市| 南宁市| 德保县| 正定县| 琼结县| 海安县| 嘉峪关市| 资阳市| 马鞍山市| 格尔木市| 安丘市| 墨竹工卡县| 鸡西市| 罗田县| 丹巴县| 平安县| 蒙自县| 团风县| 崇明县| 新闻| 望都县| 兴文县| 长泰县| 东兰县| 红原县| 广州市| 万州区| 扶风县|