TextView 的各种max 及maxEms是什么意思
来源:互联网 发布:忠邦的梦 知乎 编辑:程序博客网 时间:2024/05/22 03:31
TextView 共有五个带max的属性
maxLines
maxWidth
maxHeight
maxLength
maxEms
其中前面三个分片表示最大行数,最大宽度,最大高度,
这三个都比较简单,没什么可说的
我们主要来看看maxLength和maxEms
Section1
<TextViewandroid: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" /><TextViewandroid: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
<TextViewandroid: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" /><TextViewandroid: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" />这时候差别就显现出来了,那么,这个ems到底是个什么单位呢?我们进入源码中查看Section3
在TextView的7128行,我们找到了这么一句if (mMaxWidthMode == EMS) { width = Math.min(width, mMaxWidth * getLineHeight());} else { width = Math.min(width, mMaxWidth);}也就是说,一个em所代表的单位其实是一个行高。是这样的吗,我们来验证下代码如下:打印结果: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>09-15 15:07:45.868 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_1 length is==>24509-15 15:07:45.869 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_1 maxEms is==>509-15 15:07:45.869 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_1 lineHeight is==>4909-15 15:07:45.869 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_1 multiValue is==>24509-15 15:07:45.869 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_2 length is==>29409-15 15:07:45.869 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_2 maxEms is==>609-15 15:07:45.869 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_2 lineHeight is==>4909-15 15:07:45.869 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_2 multiValue is==>29409-15 15:07:45.869 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_3 length is==>34309-15 15:07:45.869 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_3 maxEms is==>709-15 15:07:45.869 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_3 lineHeight is==>4909-15 15:07:45.869 10594-10594/com.example.test2 I/MainActivity: onWindowFocusChanged: tv_3 multiValue is==>343
最后,我们得出结论,
maxLength 使用的单位是字符数,
maxEms 使用的单位是行高
阅读全文
0 0
- TextView 的各种max 及maxEms是什么意思
- textview中maxLength和maxEMS的区别
- Android中TextView:maxWidth、maxLength、maxEms的区别
- 【TextView】Android TextView显示省略号的问题(关于TextView maxLength 和maxEms的学习)
- TextView maxWidth maxLength maxEms 区别
- TextView maxWidth maxLength maxEms 区别
- Android 开发者,你真的会用textview(maxEms和maxLength)的属性吗?
- Android 开发者,你真的会用textview(maxEms和maxLength)的属性吗?
- Android 开发者,你真的会用textview(maxEms和maxLength)的属性吗?
- Android 开发者,你真的会用textview(maxEms和maxLength)的属性吗?
- TextView 中 android:maxEms="" ,maxLine="",maxlenght="";的区别
- 关于TextView 中设置一行显示字数的两个属性: maxEms 与 maxLength
- Android textview maxEms与maxLengt 属性
- Android TextView的各种属性
- Android TextView的各种属性
- Android TextView的各种属性
- Android TextView的各种属性
- Android TextView的各种属性
- printf函数原型
- 了解汇编之基础知识
- HDU 6153 A Secret【KMP||扩展KMP】
- 神奇的C# delegate 委托
- Bootstrap 教程
- TextView 的各种max 及maxEms是什么意思
- linux防火墙iptables常用规则(屏蔽IP地址、禁用ping、协议设置、NAT与转发、负载平衡、自定义链)
- centos 7 redis远程连接
- 反射_案例2_根据参数类型获取构造器
- jquery checkbox 全部选择或全部取消
- 5 汽水瓶子
- 双进程保活思路学习总结
- 170915_Spring英文文档阅读(九)_2.3 Usage scenarios(一)
- WebView加载速度优化