自动调整textview字体大小以适应textview长度

  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.     }  


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值