android:ellipsize="end"在ConstraintLayout中无效的问题

本文介绍了在使用ConstraintLayout时遇到android:ellipsize="end"设置不生效的问题,原因是TextView的宽度超出了屏幕。通过深入理解ConstraintLayout的约束原理,修正了TextView的宽度约束,成功实现文字省略效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在项目中使用android:ellipsize="end"无效,经过排查,发现不是没有省略号,而是已经超出了屏幕。经过分析,我觉得是对ConstraintLayout不够了解,下面贴一下我之前的代码:

	<TextView
            android:id="@+id/tv_organization"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/app_spacing_8"
            android:layout_marginStart="@dimen/app_spacing_8"
            android:ellipsize="end"
            android:singleLine="true"
            android:textColor="@color/color_bebebe"
            android:textSize="10sp"
            app:layout_constraintBottom_toBottomOf="@id/tv_time"
            app:layout_constraintLeft_toRightOf="@id/tv_time"
            tools:text="测试ellipsize测试ellipsize测试ellipsize测试ellipsize测试ellipsize测试ellipsize"/>

  </android.support.constraint.ConstraintLayout>

坑爹的是,这种方式在预览界面是没问题的,运行在手机上却出现bug
预览界面可以看到末尾的省略号
想到我之前没有在ConstraintLayout中使用过这种方式,我读了一片相关博客,大概明白了来龙去脉,ConstraintLayout中的控件都要规定约束范围,之前的写法显然违背了这个原则,textview的最大宽度等于它的父布局的宽,加上左侧别的控件的宽度,导致超出了屏幕。明白了这一层,对textview的宽度做了约束,果然实现了最终效果。这是修改后的代码:

<TextView
            android:id="@+id/tv_organization"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/app_spacing_8"
            android:layout_marginStart="@dimen/app_spacing_8"
            android:ellipsize="end"
            android:singleLine="true"
            android:textColor="@color/color_bebebe"
            android:textSize="@dimen/app_text_size_10"
            app:layout_constraintBottom_toBottomOf="@id/tv_time"
            app:layout_constraintLeft_toRightOf="@id/tv_time"
            app:layout_constraintRight_toRightOf="parent"
            tools:text="测试ellipsize测试ellipsize测试ellipsize测试ellipsize测试ellipsize测试ellipsize"/>
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值