Android中TextView内容过长未显示省略号的问题

问题描述:

按照UI设计,某个页面展示数据超长时,需要显示出省略号。
预期展示(有省略号):

实际展示(无省略号):

具体代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="          " />
    <View
        android:id="@+id/view"
        android:layout_width="1dp"
        android:layout_height="19dp"
        android:layout_weight="1"
        android:background="@color/colorAccent" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:ellipsize="end"
        android:text="测试测试测试测试测试测试测试测试测试测试测试测试测试测试" />

</LinearLayout>

原因分析:

在同事的协助下,终于发现了代码存在的问题。上面用作分隔用的View中多了坑爹的android:layout_weight="1"。关于layout_weight的原理,可以参考链接。它会导致最后将剩余的控件分配给该控件,导致了TextView的省略号显示不出来了。
PS:不过这个地方还有一个疑问,如果前面第一个TextView删除掉的话,则不会存在此问题。

解决方案:

删除View定义中多处的layout_weight属性。

附录:

https://www.cnblogs.com/net168/p/4227144.html

猜你喜欢

转载自blog.csdn.net/yinxing2008/article/details/83349944