Android TextView利用measureText自适应文本字体大小宽度
常常有这种情况,UI设计师限定一个文本TextView的宽度值比如80dip,但是该文本长度很长,于是造成文本不能正常显示,针对这种长文本长度超过TextView自身限定宽度的情况,需要把字体大小自适应的调整小。例如xml写了两个同样宽度为80dp的TextView,
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="80dp" android:layout_height="wrap_content" android:background="@android:color/holo_blue_bright" android:gravity="center" android:maxLines="1" android:text="zhangphil csdn" android:textSize="16dp" /> <TextView android:id="@+id/text" android:layout_width="80dp" android:layout_height="wrap_content" android:background="@android:color/holo_blue_bright" android:gravity="center" android:maxLines="1" android:text="zhangphil csdn" android:textSize="16dp" /> </LinearLayout>
设定textSize为16dp,显示内容是“zhangphil csdn”,但是明显,80dp宽度的TextView无法完全显示完整的“zhang phil”,所以需要调整字体大小。
setContentView(R.layout.text); TextView text = findViewById(R.id.text); //获取80dp转换后的设备pix值。 int mTextViewWidth = dip2px(this, 80); while (true) { //计算所有文本占有的屏幕宽度(pix) float textWidth = text.getPaint().measureText(text.getText().toString()); //如果所有文本的宽度超过TextView自身限定的宽度,那么就尝试迭代的减小字体的textSize,直到不超过TextView的宽度为止。 if (textWidth > mTextViewWidth) { int textSize = (int) text.getTextSize(); textSize = textSize - 2; text.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); } else { break; } } Log.d(TAG, "自适应字体的最终大小:" + text.getTextSize() + " pix");
运行输出:
04-11 09:36:03.404 29120-29120/zhangphil.test D/测试输出: 自适应字体的最终大小:34.0 pix
原理是先获得给定TextView的宽度(单位:px),如果所有文本累计的宽度超过TextView的宽度,那么久迭代的依次递减字体的textSize,试出合适的textSize为止。最终达到了自适应,限定宽度为80dp的TextView,自适应的把完整的文本装进去。作为对比,最上面的TextView没做任何自适应调整,文本就没法显示完全,少了“csdn”:
工具方法dip2px:
public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); }
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
iOS学习笔记--tableView中如何获取cell上textfiled的值
iOS学习笔记--tableView中如何获取cell上textfiled的值 最近在项目中遇到了这样一个问题,在tableView的cell上添加textfiled,然后获取cell上textfiled的值。cell的个数是可以动态改变的。如下图: 在网上看了很多别人写的没找见容易点的实现方法,自己写了一个比较笨的方法。主要代码如下: @property(nonatomic,strong)NSMutableArray * arrray//数组中元素的个数为cell的行数 @property(nonatomic,strong)NSMutableArray * tmparrray;//存cell @property(nonatomic,strong)NSMutableArray * tmpIndexPath;//存indexPath @property(nonatomic,strong)NSMutableArray * tmparrayaaaaa;//获取cell的数据 在-(UITableViewCell *)tableView:(UITableView *)tableView cel...
- 下一篇
AssetBundle使用模式
翻译自官网文档:https://unity3d.com/cn/learn/tutorials/topics/best-practices/assetbundle-usage-patterns?playlist=30089 管理加载的资产 在内存敏感的环境中仔细控制加载的对象的大小和数量非常重要。从活动场景中移除时,Unity不会自动卸载对象。资产清理在特定时间触发,也可以手动触发。 AssetBundles本身必须小心管理。由本地存储上的文件(在Unity缓存中或通过AssetBundle.LoadFromFile加载的文件)支持的AssetBundle具有最小的内存开销,很少消耗超过几十千字节。但是,如果存在大量的AssetBundles,则此开销仍可能会出现问题。 由于大多数项目允许用户重新体验内容(例如重放级别),因此知道何时加载或卸载AssetBundle非常重要。如果AssetBundle卸载不当,可能会导致内存中的对象重复。在某些情况下不当卸载AssetBundles也会导致不良行为,例如导致纹理丢失。要理解为什么会发生这种情况,请参阅资产,对象和序列化章节的“ 对象间参考...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS7设置SWAP分区,小内存服务器的救世主
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- MySQL8.0.19开启GTID主从同步CentOS8
- SpringBoot2整合Redis,开启缓存,提高访问速度
- Linux系统CentOS6、CentOS7手动修改IP地址