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
<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" />这时候差别就显现出来了,那么,这个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==>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==>24509-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==>29409-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
最后,我们得出结论,
maxLength 使用的单位是字符数,
maxEms 使用的单位是行高