TextView 隐藏了我的部分可见的最后一个词

TextView hides last word that my be patially visible

例如,我们有 2 个单词的组合:"Firstword secondword"。 Textview 有 1 行,它的宽度不大,所以第一个字是可见的,第二个字是长的以适应休息 space 并且它不可见:[Firstword______] 如何实现第二个词的部分可见性? 像这样 [Firstword secon] 或 [Firstword seco...](省略号)。可能吗? 我在一些主题中看到我想要的是使用椭圆大小时的默认行为,但在我的情况下不是。

编辑:android:ellipsize="end" android:maxLines="1" 仅在 xml 布局预览中有效,但在运行时无效。

将这些添加到您的 TextView 中

android:椭圆="end" android:maxLines="1"

<TextView
   android:id="@+id/myTextView"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:ellipsize="end"
   android:maxLines="1"/>

如果我理解你想要以编程方式设置到 textView 的内容:

textView.setEllipsize(TextUtils.TruncateAt.END);

找到解决方案。

这个xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Helloooooooooooooooooooooo Wooooooooooooooooooooooooorld!"
        android:id="@+id/tv1"
        android:textColorLink="@android:color/holo_blue_dark"
        android:ellipsize="end"
        android:singleLine="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Helloooooooooooooooooooooo Wooooooooooooooooooooooooorld!"
        android:id="@+id/tv2"
        android:textColorLink="@android:color/holo_blue_dark"
        android:ellipsize="end"
        android:lines="1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Helloooooooooooooooooooooo Wooooooooooooooooooooooooorld!"
        android:id="@+id/tv3"
        android:textColorLink="@android:color/holo_blue_dark"
        android:ellipsize="end"
        android:maxLines="1"/>
</LinearLayout>

在设计预览中渲染

好的,所有椭圆都在这里工作。 但在真实设备上它看起来像

android:singleLine="true" 仅有效,尽管它已被弃用。 在 3 台设备上测试。