如何使用AutoFitTextView [英] How to use AutoFitTextView

查看:272
本文介绍了如何使用AutoFitTextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

日子她念叨前,我发现这个code来调整内部的TextView文本。可是,我该如何使用它,它叫成我的项目。我是一个新手:)

 公共类AutoFitTextView扩展的TextView {公共AutoFitTextView(上下文的背景下){
    超级(上下文);
    在里面();
}公共AutoFitTextView(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
    在里面();
}私人无效的init(){    maxTextSize = this.getTextSize();
    如果(maxTextSize&下; 35){
        maxTextSize = 30;
    }
    minTextSize = 20;
}私人无效refitText(字符串文本,整数输出textWidth){
   如果(输出textWidth 0){
       INT availableWidth =输出textWidth - this.getPaddingLeft()
                - this.getPaddingRight();
       浮trySize = maxTextSize;       this.setTextSize(TypedValue.COMPLEX_UNIT_PX,trySize);
       而((trySize minTextSize)
               &功放;&安培; (this.getPaint()。measureText(文本)availableWidth)){
           trySize - = 1;
           如果(trySize< = minTextSize){
               trySize = minTextSize;
               打破;
           }
           this.setTextSize(TypedValue.COMPLEX_UNIT_PX,trySize);
       }
       this.setTextSize(TypedValue.COMPLEX_UNIT_PX,trySize);
   }}@覆盖
保护无效之前onTextChanged(最终CharSequence的文字,最终诠释开始,最终诠释,
        后最终诠释){
    refitText(text.toString(),this.getWidth());
}@覆盖
保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
    如果(W!= oldw){
        refitText(this.getText()的toString(),W);
    }
}@覆盖
保护无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec){
    super.onMeasure(widthMeasureSpec,heightMeasureSpec);
    INT上级宽度= MeasureSpec.getSize(widthMeasureSpec);
    refitText(this.getText()的toString(),上级宽度。);
}公众持股量getMinTextSize(){
    返回minTextSize;
}公共无效setMinTextSize(INT minTextSize){
    this.minTextSize = minTextSize;
}公众持股量getMaxTextSize(){
    返回maxTextSize;
}公共无效setMaxTextSize(INT minTextSize){
    this.maxTextSize = minTextSize;
}私人浮动minTextSize;私人浮动maxTextSize;}


解决方案

在你的布局,您可以使用它像一个典型的的TextView ,除非你指向的位置,你的 AutoFitTextView 类。

 < com.your.package.name.AutoFitTextView
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT/>

和在code:

  AutoFitTextView富=(AutoFitTextView)findViewById(ID);

您真正需要做的是你的布局中使用它。如果你没有你的布局中使用它,那么就不要在你使用code两种。所以,无论是在布局和code,只有布局,或根本不使用它。

此外,在布局确保你poiting到正确的位置。我不知道你有什么包在你的 AutoFitTextView 类,所以我只用 com.your.package.name 作为一个例子。

就是这样,很容易做的。

Somedays ago, I found this code to resize text inside TextView. But, how can I use it and call it into my project. Im a newbie :)

public class AutoFitTextView extends TextView {

public AutoFitTextView(Context context) {
    super(context);
    init();
}

public AutoFitTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

private void init() {

    maxTextSize = this.getTextSize();
    if (maxTextSize < 35) {
        maxTextSize = 30;
    }
    minTextSize = 20;
}

private void refitText(String text, int textWidth) {
   if (textWidth 0) {
       int availableWidth = textWidth - this.getPaddingLeft()
               - this.getPaddingRight();
       float trySize = maxTextSize;

       this.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
       while ((trySize minTextSize)
               && (this.getPaint().measureText(text) availableWidth)) {
           trySize -= 1;
           if (trySize <= minTextSize) {
               trySize = minTextSize;
               break;
           }
           this.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
       }
       this.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
   } }

@Override
protected void onTextChanged(final CharSequence text, final int start, final int before,
        final int after) {
    refitText(text.toString(), this.getWidth());
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    if (w != oldw) {
        refitText(this.getText().toString(), w);
    }
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
    refitText(this.getText().toString(), parentWidth);
}

public float getMinTextSize() {
    return minTextSize;
}

public void setMinTextSize(int minTextSize) {
    this.minTextSize = minTextSize;
}

public float getMaxTextSize() {
    return maxTextSize;
}

public void setMaxTextSize(int minTextSize) {
    this.maxTextSize = minTextSize;
}

private float minTextSize;

private float maxTextSize;

}

解决方案

In your layout, you use it like a typical TextView except you point to the location of your AutoFitTextView class.

 <com.your.package.name.AutoFitTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

And in code:

AutoFitTextView foo = (AutoFitTextView) findViewById(id);

All you really need to do is use it within your layout. If you don't use it within your layout, then don't use it within you code either. So, either use it in your layout and code, only layout, or not at all.

Also, in your layout make sure you're poiting to the correct location. I don't know what package you have your AutoFitTextView class in, so I only used com.your.package.name as an example.

That's it, easy does it.

这篇关于如何使用AutoFitTextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆