需求:

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

使用框架:Android-autofittextview
地址:https://github.com/grantland/android-autofittextview

使用方式详见官网介绍。

例子展示:

可以看出来:当文字没有填充TextView完全时显示的就是默认的字体,当文字能够完全填充TextView并且一行显示不下时,他会默认的缩小文字的字体,当文字再多时,他会默认在末尾省略。


AutofitTextView.java代码:

package 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* @attr ref R.styleable.AutofitTextView_precision*/
public 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}*/@Overridepublic void setTextSize(int unit, float size) {super.setTextSize(unit, size);if (mHelper != null) {mHelper.setTextSize(unit, size);}}/*** {@inheritDoc}*/@Overridepublic void setLines(int lines) {super.setLines(lines);if (mHelper != null) {mHelper.setMaxLines(lines);}}/*** {@inheritDoc}*/@Overridepublic 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 sizeToFit*/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 ref android.R.styleable#TextView_textSize*/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 ref android.R.styleable#TextView_textSize*/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 ref me.grantland.R.styleable#AutofitTextView_minTextSize*/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 ref me.grantland.R.styleable#AutofitTextView_minTextSize*/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 precision The amount of precision.*/public void setPrecision(float precision) {mHelper.setPrecision(precision);}@Overridepublic 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();elser = c.getResources();setRawTextSize(TypedValue.applyDimension(unit, size, r.getDisplayMetrics()));}

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配置文件默认保存一致。

参考:http://blog.csdn.net/qq_23547831/article/details/50592352

Android动态更改TextView的字体大小相关推荐

  1. Android动态改变TextView字体颜色

    Android动态改变TextView字体颜色 分类: Android 2012-06-04 21:56 141人阅读 评论(0) 收藏 举报 androidcolorslayout 必须在在res/ ...

  2. Android TextView自动调整字体大小(官方)

    官方在 Android 8.0(API26)给TextView,新增字体大小随着控件的大小变化的特性. 如果使用 appcompat 兼容库,可以向下支持到Android 4.0(AP14). 1.自 ...

  3. android auto 字体大小,TextView自动调整字体大小

    系统SDK版本大于等于26,直接使用TextView就可以. 系统SDK版本小于26,需要使用support包,support包的版本要大于等于26.0.support包支持Android 4.0 ( ...

  4. android webview 字体 系统字体大小,【转】android webview设置内容的字体大小

    Enum for specifying the text size. SMALLEST is 50% SMALLER is 75% NORMAL is 100% LARGER is 150% LARG ...

  5. Android getDimensionPixelSize, 代码中设置字体大小,读xml配置。

    //给一个id为name的TextView设置字体大小  TextView mName = (TextView)findViewById(R.id.name);  mName.setTextSize( ...

  6. 如何在Android中更改TextView的fontFamily

    本文翻译自:How to change fontFamily of TextView in Android So I'd like to change the android:fontFamily i ...

  7. 移动端开发使用rem时动态设置html的字体大小

    1.动态设置html的字体大小 移动端开发时习惯使用rem单位,而rem是基于根html的字体大小的,为了适配不同尺寸屏幕需要动态设置html字体的大小.我们一般习惯于设置html的字体大小是基于iP ...

  8. 更改计算机菜单字体大小,windows10系统修改右键菜单字体的方法

    默认情况下,每台win10系统电脑的字体都是一样的,不过一些用户由于个人需求,就想要进行更改.该如何操作呢?今天,系统城小编将在下面给大家分享windows10系统修改右键菜单字体的方法.有需要的朋友 ...

  9. SpringToolSuite4更改编辑区字体大小

    SpringToolSuite4使用记录 1.更改编辑区字体大小 菜单栏Windows–preferences 按照路径General–Appearance–Color and Fonts–Basic ...

最新文章

  1. 再爆安全漏洞,这次轮到Jackson了,竟由阿里云上报
  2. 图像降噪算法——稀疏表达:K-SVD算法
  3. ASP.NET Core中实现单体程序的事件发布/订阅 - LamondLu - 博客园
  4. apache pulsar_Apache Pulsar:分布式Pub-Sub消息系统
  5. 深度学习(2)--常见概率分布(2)
  6. 商务英语计算机,BEC商务英语
  7. ZZULIOJ 1124: 两个有序数组合并
  8. centos7 配置虚拟交换机(物理交换机truck端口设置)(使用brctl)
  9. C++函数free和delete如何操作指针?
  10. python下载-python
  11. python参考手册第四版_Python参考手册 PDF 第4版
  12. 【宠物领养系统项目】(附源码)
  13. (2021.10.25-10.31)小结
  14. 计算机软件服务费入哪个科目,软件服务费计入什么科目?
  15. PYTHON单任务FTP断点续传程序
  16. ios 系统状态栏样式修改_iOS 导航栏颜色和状态栏颜色修改
  17. yocto编译错误:ERROR: libtinyxml2-XXX do_fetch: Fetcher failure for URL
  18. echarts折线图鼠标移入页面出现抖动
  19. 主动形状模型(ASM)和主动外观模型(AAM)资源汇总
  20. python隐藏手机号码程序_Python网站手机号码解密的一种方法

热门文章

  1. uni-app学习日记1
  2. CodeVS2505 上学路线
  3. OpenCV交叉编译,选项不同同样成功的路子
  4. linux环境下gitea使用,linux一键安装gitea
  5. 使用idea连接阿里云RDS数据库
  6. 苹果5完美越狱_checkra1n for Mac(越狱工具)
  7. 如何使用 FFmpeg 进行视频转码:字幕
  8. 人工智能作用现代认知战探析
  9. svm算法 最通俗易懂讲解
  10. mysql wait for flush,Mysql线程大量Wating For table flush问题分析