Android动态更改TextView的字体大小

网友投稿 1464 2022-09-28

Android动态更改TextView的字体大小

Android动态更改TextView的字体大小

需求:

需要动态更改TextView内容字体的大小,比如设定TextView只有一行,宽度只有200dp,内容超过这个之后就缩小字体显示,只能能将字体都显示完全;也就是动态更改TextView的字体大小,当TextView的内容比较多时缩小显示,当TextView的内容比较少时正常显示。

使用框架:Android-autofittextview 地址:​​​me.grantland.widget;import android.content.Context;import android.util.AttributeSet;import android.util.TypedValue;import android.widget.TextView;/** * A {@link TextView} that re-sizes its text to be no larger than the width of the view. * * @attr ref R.styleable.AutofitTextView_sizeToFit * @attr ref R.styleable.AutofitTextView_minTextSize * @attrpublic class AutofitTextView extends TextView implements AutofitHelper.OnTextSizeChangeListener private AutofitHelper mHelper; public AutofitTextView(Context context) { super(context); init(context, null, 0); } public AutofitTextView(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs, 0); } public AutofitTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context, attrs, defStyle); } private void init(Context context, AttributeSet attrs, int defStyle) { mHelper = AutofitHelper.create(this, attrs, defStyle) .addOnTextSizeChangeListener(this); } // Getters and Setters /** * {@inheritDoc} */ @Override public void setTextSize(int unit, float size) { super.setTextSize(unit, size); if (mHelper != null) { mHelper.setTextSize(unit, size); } } /** * {@inheritDoc} */ @Override public void setLines(int lines) { super.setLines(lines); if (mHelper != null) { mHelper.setMaxLines(lines); } } /** * {@inheritDoc} */ @Override public void setMaxLines(int maxLines) { super.setMaxLines(maxLines); if (mHelper != null) { mHelper.setMaxLines(maxLines); } } /** * Returns the {@link AutofitHelper} for this View. */ public AutofitHelper getAutofitHelper() { return mHelper; } /** * Returns whether or not the text will be automatically re-sized to fit its constraints. */ public boolean isSizeToFit() { return mHelper.isEnabled(); } /** * Sets the property of this field (sizeToFit), to automatically resize the text to fit its * constraints. */ public void setSizeToFit() { setSizeToFit(true); } /** * If true, the text will automatically be re-sized to fit its constraints; if false, it will * act like a normal TextView. * * @param public void setSizeToFit(boolean sizeToFit) { mHelper.setEnabled(sizeToFit); } /** * Returns the maximum size (in pixels) of the text in this View. */ public float getMaxTextSize() { return mHelper.getMaxTextSize(); } /** * Set the maximum text size to the given value, interpreted as "scaled pixel" units. This size * is adjusted based on the current density and user font size preference. * * @param size The scaled pixel size. * * @attr public void setMaxTextSize(float size) { mHelper.setMaxTextSize(size); } /** * Set the maximum text size to a given unit and value. See TypedValue for the possible * dimension units. * * @param unit The desired dimension unit. * @param size The desired size in the given units. * * @attr public void setMaxTextSize(int unit, float size) { mHelper.setMaxTextSize(unit, size); } /** * Returns the minimum size (in pixels) of the text in this View. */ public float getMinTextSize() { return mHelper.getMinTextSize(); } /** * Set the minimum text size to the given value, interpreted as "scaled pixel" units. This size * is adjusted based on the current density and user font size preference. * * @param minSize The scaled pixel size. * * @attr public void setMinTextSize(int minSize) { mHelper.setMinTextSize(TypedValue.COMPLEX_UNIT_SP, minSize); } /** * Set the minimum text size to a given unit and value. See TypedValue for the possible * dimension units. * * @param unit The desired dimension unit. * @param minSize The desired size in the given units. * * @attr public void setMinTextSize(int unit, float minSize) { mHelper.setMinTextSize(unit, minSize); } /** * Returns the amount of precision used to calculate the correct text size to fit within its * bounds. */ public float getPrecision() { return mHelper.getPrecision(); } /** * Set the amount of precision used to calculate the correct text size to fit within its * bounds. Lower precision is more precise and takes more time. * * @param public void setPrecision(float precision) { mHelper.setPrecision(precision); } @Override public void onTextSizeChange(float textSize, float oldTextSize) { // do nothing

原理:

AutofitTextView:自定义TextView并继承系统的TextView,然后在绘制组件的时候根据getMaxLines方法获取内容的行数若内容的行数大于1,则缩小文字的字体,然后在尝试获取getMaxLines方法,若内容的行数还是大于1,则缩小文字的字体,直到内容能够一行显示或者是字体缩小大一定的大小,这时候若缩小到一定的大小还是不能一行显示,则尾部省略。

延伸: TextView源码中:

public void setTextSize(float size) { setTextSize(TypedValue.COMPLEX_UNIT_SP, size); }public void setTextSize(int unit, float size) { Context c = getContext(); Resources r; if (c == null) r = Resources.getSystem(); else

setTextSize(int unit, int size) 第一个参数可设置如下静态变量: TypedValue.COMPLEX_UNIT_PX : Pixels TypedValue.COMPLEX_UNIT_SP : Scaled Pixels TypedValue.COMPLEX_UNIT_DIP : Device Independent Pixels

我们在使用TextView控件时,调用setTextSize方法,默认就是以sp为单位,与xml配置文件默认保存一致。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot中使用HTTP客户端工具Retrofit
下一篇:在哪里能下载纯净的系统镜像?让我来告诉你……
相关文章

 发表评论

暂时没有评论,来抢沙发吧~