2016-11-18 84 views
-1

例如,我们有2个单词的组合:“Firstword secondword”。 Textview有1行,宽度不大,所以第一个单词是可见的,第二个长到适合休息空间,它不可见:[Firstword______] 如何实现第二个单词的部分可见性? 像这样[Firstword secon]或[Firstword seco ...](带椭圆大小)。可能吗? 我在一些主题中看到,我想要的是使用ellipsize时的默认行为,但在我的情况下不是。TextView隐藏最后一个词,我可以大致可见

编辑:android:ellipsize =“end”android:maxLines =“1”只能在xml布局预览中使用,但不能在运行时使用。

回答

1

找到解决方案。

这个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> 

渲染设计预览像 enter image description here

OK,所有ellipsizes在这里工作。 但是在真实设备上,它看起来像 enter image description here

android:singleLine =“true”只有工作,尽管不推荐使用。 在3个设备上测试。

1

在你的TextView添加这些

机器人:ellipsize = “结束” 机器人:MAXLINES = “1”

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

是的,它的工作在xml布局预览。 但在运行时不起作用。显示[Firstword ...____] 这可能是因为我使用自定义的TextView和自己的ttf字体? –

+0

textView.setEllipsize(TextUtils.TruncateAt.END); – PravinCG

+0

textView.setEllipsize(TextUtils.TruncateAt.END)不起作用。 即使使用默认的TextView。 –

0

如果我理解你想要的设置TextView的编程这:

textView.setEllipsize(TextUtils.TruncateAt.END); 
+0

这不工作,我不明白为什么... –