自動調整textview字型大小以適應textview長度

來源:互聯網
上載者:User
  1. package com.test.android.textview;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Paint;  
  5. import android.util.AttributeSet;  
  6. import android.widget.TextView;  
  7.   
  8. public class CustomTextView extends TextView {  
  9.   
  10.     private static float DEFAULT_MIN_TEXT_SIZE = 10;  
  11.     private static float DEFAULT_MAX_TEXT_SIZE = 20;  
  12.   
  13.     // Attributes  
  14.     private Paint testPaint;  
  15.     private float minTextSize, maxTextSize;  
  16.   
  17.     public CustomTextView(Context context, AttributeSet attrs) {  
  18.         super(context, attrs);  
  19.         initialise();  
  20.     }  
  21.   
  22.     private void initialise() {  
  23.         testPaint = new Paint();  
  24.         testPaint.set(this.getPaint());  
  25.   
  26.         // max size defaults to the intially specified text size unless it is  
  27.         // too small  
  28.         maxTextSize = this.getTextSize();  
  29.   
  30.         if (maxTextSize <= DEFAULT_MIN_TEXT_SIZE) {  
  31.             maxTextSize = DEFAULT_MAX_TEXT_SIZE;  
  32.         }  
  33.   
  34.         minTextSize = DEFAULT_MIN_TEXT_SIZE;  
  35.     };  
  36.   
  37.     /** 
  38.      * Re size the font so the specified text fits in the text box * assuming 
  39.      * the text box is the specified width. 
  40.      */  
  41.     private void refitText(String text, int textWidth) {  
  42.         if (textWidth > 0) {  
  43.             int availableWidth = textWidth - this.getPaddingLeft()  
  44.                     - this.getPaddingRight();  
  45.             float trySize = maxTextSize;  
  46.             testPaint.setTextSize(trySize);  
  47.             while ((trySize > minTextSize)  
  48.                     && (testPaint.measureText(text) > availableWidth)) {  
  49.                 trySize -= 1;  
  50.                 if (trySize <= minTextSize) {  
  51.                     trySize = minTextSize;  
  52.                     break;  
  53.                 }  
  54.                 testPaint.setTextSize(trySize);  
  55.             }  
  56.             this.setTextSize(trySize);  
  57.         }  
  58.     };  
  59.   
  60.     @Override  
  61.     protected void onTextChanged(CharSequence text, int start, int before,  
  62.             int after) {  
  63.         super.onTextChanged(text, start, before, after);  
  64.         refitText(text.toString(), this.getWidth());  
  65.     }  
  66.   
  67.     @Override  
  68.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  69.         if (w != oldw) {  
  70.             refitText(this.getText().toString(), w);  
  71.         }  
  72.     }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.