【问题标题】:How can I show ellipses on my TextView if it is greater than the 1 line?如果 TextView 大于 1 行,如何在 TextView 上显示省略号?
【发布时间】:2011-09-17 14:53:12
【问题描述】:

我的以下布局不起作用:

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:id="@+id/experienceLayout" 
    android:background="#ffffff" 
    android:layout_height="match_parent" 
    android:paddingLeft="6dp" 
    android:paddingRight="6dp" 
    android:paddingBottom="6dp" 
    android:paddingTop="6dp">

    <TextView 
        android:layout_weight="1" 
        android:id="@+id/experienceLabel" 
        android:text="Experience" 
        android:layout_height="wrap_content" 
        android:textColor="#000000" 
        android:layout_width="wrap_content" 
        android:textStyle="bold">
    </TextView>

    <TextView 
        android:id="@+id/experienceTextView" 
        android:text="TextView" 
        android:layout_height="wrap_content" 
        android:textColor="#000000" 
        android:layout_width="wrap_content" 
        android:ellipsize="end" 
        android:lines="1" 
        android:maxLines="1" 
        android:singleLine="true" 
        android:fadeScrollbars="false">
    </TextView>

</LinearLayout>

【问题讨论】:

    标签: java android android-linearlayout textview


    【解决方案1】:

    这是一个常见问题。尝试使用以下内容:

    android:scrollHorizontally="true"
    android:ellipsize="end" 
    android:maxLines="1"
    

    ........ scrollHorizo​​ntally 是使它工作的“特殊调味料”。

    【讨论】:

    • 奇怪...我试过android:scrollHorizontally="true"但没用,我不得不使用已弃用的属性android:singleLine="true"
    • 是的 scrollHorizontally... 的实际密钥
    • 水平滚动?显然我不想要水平滚动效果。
    • @user1232726:是的,“水平滚动”。作为常识,请查看问题的日期和回复的日期......不用说,目前两者都可能无关紧要(在发表评论之前)。
    • @VaishnavMhetre 我只是实现了上面的解决方案,根本没有崩溃。
    【解决方案2】:

    这也会用椭圆形成一条直线

     android:singleLine="true"
    

    【讨论】:

    • 这显然已被弃用
    • 这个更好打断长词。
    • android:maxLines="1"
    • @grebulon 它已被弃用。至少现在。
    【解决方案3】:

    使用这个

    android:ellipsize="end"  
    android:singleLine="true"
    

    在没有完全了解输出的情况下不要使用它

    android:ellipsize="end"  
    android:maxLines="1"
    

    当您使用maxlines = 1 时,它会在一段时间内截断大部分字符。

    【讨论】:

      【解决方案4】:

      它在多个设备/API 上为我工作的方式是这样编程的(其中 tv 是你的 TextView):

          if (tv.getLineCount() > 1) {
              int lineEndIndex = tv.getLayout().getLineEnd(0);
              String text = tv.getText().subSequence(0, lineEndIndex - 3) + "\u2026";
              tv.setText(text);
          }
      

      【讨论】:

      • 这是最有用的答案...适用于每个 API,并且可以轻松地将其转换为 Utils 库。
      • 您应该使用省略号字符\u2026 而不是三个. 字符
      • 你是对的@ChrisStillwell,我确实在我的代码中使用了省略号字符。我已经编辑了答案,谢谢。 :)
      【解决方案5】:

      所以上面的所有答案都满足了只出现 1 行然后出现省略号的要求。但是,如果您希望省略号出现在某些文本行之后,则应使用以下内容:

      android:ellipsize="end"
      android:maxLines="2"
      android:singleLine="false"
      

      这样省略号只会出现在 2 行之后。注意:将 singleLine 设为 false 很重要。

      【讨论】:

      【解决方案6】:

      这对我有帮助:

      android:ellipsize="end"
      android:maxLines="1"
      android:singleLine="true"
      

      确保 TextView width 设置为 Match_Parent

      https://github.com/chrisjenx/Calligraphy/issues/43#issuecomment-523701518

      【讨论】:

        【解决方案7】:

        android:singleLine 已弃用。在我的例子中,我必须为TextView 获得一个固定的高度,并且我使用了android:lines 属性而不是android:maxLines。我认为这可能会帮助遇到与我相同问题的人。

        android:ellipsize="end"
        android:lines="2"
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-11-21
        • 2011-09-23
        • 1970-01-01
        • 2013-09-15
        • 1970-01-01
        • 1970-01-01
        • 2020-10-21
        相关资源
        最近更新 更多