TextView 的各种max 及maxEms是什么意思
TextView 共有五个带max的属性
maxLines
maxWidth
maxHeight
maxLength
maxEms
其中前面三个分片表示最大行数,最大宽度,最大高度,
这三个都比较简单,没什么可说的
我们主要来看看maxLength和maxEms
Section1
<TextView android:maxLength="5" android:onClick="goToNext" android:id="@+id/tv_1" android:maxLines="1" android:text="我是一个很长的文本" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:maxEms="5" android:onClick="goToNext" android:id="@+id/tv_2" android:maxLines="1" android:text="我是一个很长的文本" android:layout_width="wrap_content" android:layout_height="wrap_content" />
这个时候,两者都是中文,运行起来对于文本长高度的限制是没有区别的,
我们改成英文字符试试
Section2
这时候差别就显现出来了,
那么,这个ems到底是个什么单位呢?
我们进入源码中查看
Section3
在TextView的7128行,我们找到了这么一句
也就是说,一个em所代表的单位其实是一个行高。
是这样的吗,我们来验证下
代码如下:
09-15 15:07:45.868 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_1 length is==>245 09-15 15:07:45.869 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_1 maxEms is==>5 09-15 15:07:45.869 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_1 lineHeight is==>49 09-15 15:07:45.869 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_1 multiValue is==>245
09-15 15:07:45.869 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_2 length is==>294 09-15 15:07:45.869 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_2 maxEms is==>6 09-15 15:07:45.869 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_2 lineHeight is==>49 09-15 15:07:45.869 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_2 multiValue is==>294
09-15 15:07:45.869 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_3 length is==>343 09-15 15:07:45.869 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_3 maxEms is==>7 09-15 15:07:45.869 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_3 lineHeight is==>49 09-15 15:07:45.869 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_3 multiValue is==>343
打印结果:
public class MainActivity extends Activity {private TextView tv_1;private TextView tv_2;private TextView tv_3;@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv_1 = (TextView) findViewById(R.id.tv_1);tv_2 = (TextView) findViewById(R.id.tv_2);tv_3 = (TextView) findViewById(R.id.tv_3);}@Override public void onWindowFocusChanged(boolean hasFocus) {super.onWindowFocusChanged(hasFocus);Log.i(getClass().getSimpleName(),"onWindowFocusChanged: tv_1 length is==>"+tv_1.getWidth());Log.i(getClass().getSimpleName(),"onWindowFocusChanged: tv_1 maxEms is==>"+tv_1.getMaxEms());Log.i(getClass().getSimpleName(),"onWindowFocusChanged: tv_1 lineHeight is==>"+tv_1.getLineHeight());Log.i(getClass().getSimpleName(),"onWindowFocusChanged: tv_1 multiValue is==>"+tv_1.getLineHeight()*tv_1.getMaxEms());Log.i(getClass().getSimpleName(),"onWindowFocusChanged: tv_2 length is==>"+tv_2.getWidth());Log.i(getClass().getSimpleName(),"onWindowFocusChanged: tv_2 maxEms is==>"+tv_2.getMaxEms());Log.i(getClass().getSimpleName(),"onWindowFocusChanged: tv_2 lineHeight is==>"+tv_2.getLineHeight());Log.i(getClass().getSimpleName(),"onWindowFocusChanged: tv_2 multiValue is==>"+tv_2.getLineHeight()*tv_2.getMaxEms());Log.i(getClass().getSimpleName(),"onWindowFocusChanged: tv_3 length is==>"+tv_3.getWidth());Log.i(getClass().getSimpleName(),"onWindowFocusChanged: tv_3 maxEms is==>"+tv_3.getMaxEms());Log.i(getClass().getSimpleName(),"onWindowFocusChanged: tv_3 lineHeight is==>"+tv_3.getLineHeight());Log.i(getClass().getSimpleName(),"onWindowFocusChanged: tv_3 multiValue is==>"+tv_3.getLineHeight()*tv_3.getMaxEms());}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" tools:context="com.example.test2.MainActivity">android:layout_height="wrap_content" /><TextView android:maxEms="5" android:onClick="goToNext" android:id="@+id/tv_1" android:maxLines="1" android:text="汉字汉字汉字汉字汉字汉字汉字" android:layout_width="wrap_content" android:layout_height="wrap_content" /><TextView android:maxEms="6" android:onClick="goToNext" android:id="@+id/tv_2" android:maxLines="1" android:text="abcdefghijklmnopqrst" android:layout_width="wrap_content" android:layout_height="wrap_content" /><TextView android:maxEms="7" android:onClick="goToNext" android:id="@+id/tv_3" android:maxLines="1" android:text="a中b英c混d合ea中b英c混d合ea中b英c混d合e" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
if (mMaxWidthMode == EMS) {width = Math.min(width, mMaxWidth * getLineHeight()); } else {width = Math.min(width, mMaxWidth); }
<TextView android:maxLength="5" android:onClick="goToNext" android:id="@+id/tv_1" android:maxLines="1" android:text="abcdefghijklmnopqrst" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:maxEms="5" android:onClick="goToNext" android:id="@+id/tv_2" android:maxLines="1" android:text="abcdefghijklmnopqrst" android:layout_width="wrap_content" android:layout_height="wrap_content" />
最后,我们得出结论,
maxLength 使用的单位是字符数,
maxEms 使用的单位是行高
TextView 的各种max 及maxEms是什么意思相关推荐
- Android中TextView:maxWidth、maxLength、maxEms的区别
1.maxWidth maxWidth指的是控件自己的最大宽度,当控件的属性layout_width="wrap_content"时生效,若layout_width指定了具体的值, ...
- 【TextView】Android TextView显示省略号的问题(关于TextView maxLength 和maxEms的学习)
1.TextView的属性maxLength和maxEms 1). maxLength 作用:设置TextView 文本显示的个数 理解:把text的字符取出来,截取了指定个数个字符,然后将截取的字符 ...
- 记录《时间进度》(人生进度条)(包含自定义进度、小插件)的开发
前言 开发的本意是想看到可视化的时间,让我们心存敬畏,珍惜时间,毕竟时间一去不复返! 最近灵感突突然袭来,想着做一个时间精度条:今天度过了多少,这周度过了多少,这个月度过了多少,今年度过了多少(以及这 ...
- 国内android开发之墙痛,Android面试题-机型适配之痛,例如三星、小米、华为、魅族等。...
自定义控件 联网 工具 数据库 源码分析相关面试题 Activity相关面试题 Service相关面试题 与XMPP相关面试题 与性能优化相关面试题 与登录相关面试题 与开发相关面试题 与人事相关面试 ...
- Android面试题-机型适配之痛,例如三星、小米、华为、魅族等。
源码分析相关面试题 Volley源码分析 注解框架实现原理 okhttp3.0源码分析 onSaveInstanceState源码分析 Activity相关面试题 保存Activity的状态 acti ...
- progressbar
(一) 1.效果图:点击增加进度条增加,点击减少,进度条减少,点击重置,进度条恢复到最初样子 2.activity_main.xml 1 <?xml version="1.0" ...
- Android面试题-机型适配之痛,例如三星、小米、华为、魅族系统问题处理方式
源码分析相关面试题 Volley源码分析 注解框架实现原理 okhttp3.0源码分析 onSaveInstanceState源码分析 Activity相关面试题 保存Activity的状态 acti ...
- Android通知栏介绍与适配总结
由于历史原因,Android在发布之初对通知栏Notification的设计相当简单,而如今面对各式各样的通知栏玩法,谷歌也不得不对其进行更新迭代调整,增加新功能的同时,也在不断地改变样式,试图迎合更 ...
- Android 开发者,你真的会用textview(maxEms和maxLength)的属性吗?
这里我们不说那些复杂的属性,光说我们通常用的比较多的,android:maxlength 官网API对其的解释为: 第一句,也就是说,他是个inputfilter(输入过滤器)他的作用是通过speci ...
最新文章
- Dos一键清理系统垃圾教程
- linux ssh客户端工具
- 【clickhouse】clickhouse时区
- linux档案与文件的的压缩与打包
- spring aop中使用@Pointcut注解抽取可重用的切入点表达式
- 服务器文件传输抓包,Post入门篇 第12课multipart/form-data多部件上传抓包分析过程...
- Day 23 What Drivers You Crazy
- walking机器人入门教程-软件清单
- SCI论文结构化阅读法
- 【c语言】复习无止境,day4--堆内存宏函数篇
- 数学之路-python计算实战(4)-Lempel-Ziv压缩(1)
- Java—初识Java与开发环境的安装
- 设置vlan虚拟局域网
- 呆萌程序猿的恋爱奇葩说
- 再度盈利后提“冷静增长”,爱奇艺守住长视频初心
- 基于javaweb的公寓房屋出租系统(java+ssm+jsp+easyui+echarts+mysql)
- 葡萄牙生产一台计算机,外贸教程-国际贸易教程作业题如果美国生产一台计算机需要60小时的劳动,生产 爱问知识人...
- 【已解决】Windows在设置屏幕保护程序显示灰色,更改不了
- paylinks.php_私服发布网站PHP程序(含系统安装教程)
- INTEL RealSense-D415 在 Ubuntu 16.04 开发流程 1
热门文章
- 猿创征文|瑞吉外卖——移动端_邮箱验证码登录
- 写出HTML的基本结构 做简要说明,北京市顺义区2017年--2018年届高三二模语文试题(卷)与答案解析.doc...
- CSS3之好看的特效
- python —— 使用sympy模块求解数学方程
- 抖音上的python课程_如何用Python抓抖音上的小姐姐
- 查看linux最大的sftp连接数,Linux查看某个端口的连接数的方法
- BUUCTF 每日打卡 2021-4-5
- C语言 弹小球 小游戏(控制台)
- 误删除与误格式化的挽回(图)
- 【二分答案】买礼物的艰辛