效果图:
image.png
1:布局
<TextView
android:id="@id/tvTest"
android:paddingLeft="@dimen/p10"
android:paddingRight="@dimen/p10"
android:text="上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上上的冯绍峰上"
android:layout_width="match_parent"
android:maxLines="2"
android:ellipsize="end"
android:layout_height="wrap_content"/>
2:布局中添加以下配置无效,需要添加第三步的代码
3:需要在activity中设置以下代码:
if (tvTest.lineCount > 2){
val lineEndIndex = tvTest.layout.getLineEnd(1) //拿取第二行最后一个字符的位置
val text = "" + (tvTest.text.subSequence(0,lineEndIndex)) + "..." //进行截取+拼接
tvTest.text =text
}
实现以上三步就可以实现以上的效果图
网友评论