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
<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==>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

最后,我们得出结论,
maxLength 使用的单位是字符数,
maxEms    使用的单位是行高



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值