国产探花免费观看_亚洲丰满少妇自慰呻吟_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());   } } 

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 闵行区| 上高县| 威信县| 永胜县| 齐河县| 黔东| 乌审旗| 宁津县| 阆中市| 茶陵县| 沅陵县| 信阳市| 鄂托克前旗| 平阳县| 黑山县| 城市| 崇信县| 延津县| 蛟河市| 吉安市| 玛纳斯县| 临潭县| 泾川县| 贡山| 高邑县| 翼城县| 平武县| 佛冈县| 蓬安县| 江西省| 鹤岗市| 阿拉善盟| 霍邱县| 加查县| 凉城县| 遂昌县| 丹凤县| 靖宇县| 响水县| 陵水| 木兰县|