android: auto-scaling textView in dialog

独自空忆成欢 提交于 2019-12-25 08:56:58

问题


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!


回答1:


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"


来源:https://stackoverflow.com/questions/20683272/android-auto-scaling-textview-in-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!