Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

android: auto-scaling textView in dialog

I am trying to set the TextView to be auto scale inside the dialog,

xml

    <com.abc.abc.AutoResizeTextView
        android:id="@+id/text_btn"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_above="@+id/view2"
        android:layout_alignLeft="@+id/linear_layout_color1"
        android:layout_alignRight="@+id/linear_layout_color1"
        android:layout_alignTop="@+id/doodleView"
        android:layout_margin="1dp"
        android:background="@color/white"
        android:gravity="center"
        android:text="A"
        android:textColor="@color/grey" />

AutoResizeTextView

and AutoResizeTextView as in

Auto Scale TextView Text to Fit within Bounds

and then in java code

    AutoResizeTextView  textView = (AutoResizeTextView ) writing_dialog.findViewById(R.id.text_btn);
    textView.setText("B");

Question:

I do not know why the text B is still appearing very small and not auto-scaling...

Thanks for your advice!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I am using this version of auto fit text view, which works great:

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;

}

also set the width to:

android:layout_width="match_parent"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...