简体   繁体   English

Android:在 TextView 中截取长多行文本

[英]Android: Cut off to long multiline text in TextView

I have a TextView with a height depending on previous content, but there might be a long Text in it.我有一个高度取决于先前内容的 TextView,但其中可能有很长的 Text。 How can I cut it off at the right point and concatenate three dots or something similar to the new end?我怎样才能在正确的点切断它并连接三个点或类似于新末端的东西?

Currently it looks like this:目前它看起来像这样: 被切断的多行文本

I found some solutions for Single Line Text, but how does it work with more than one line?我找到了一些单行文本的解决方案,但它如何处理多行文本? I also do not know the number of lines, because this depends on the screen size.我也不知道行数,因为这取决于屏幕大小。

Are there other typical ways on Android to show that the text can be extended? Android 上还有其他典型的方式来显示文本可以扩展吗? Eg a colour gradient in the last line?例如,最后一行的颜色渐变?

Edit:编辑:

When I do it without a fixed heigth, I have to make the height depend on the element above and my XML will look like this:当我在没有固定高度的情况下执行此操作时,我必须使高度取决于上面的元素,并且我的 XML 将如下所示:

<TextView
   android:id="@+id/podcastShortDesc"
   android:text="Long text"
   android:layout_below="@+id/podcastTitle"
   android:layout_toRightOf="@+id/podcastLogo"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"

   android:maxLines="3"
   android:ellipsize="end"

   android:layout_above="@+id/podcastMoreAction" />

When I do specify maxLines I can have luck an it will work:当我指定 maxLines 时,我会很幸运,它会起作用: 我有运气

But if the title is too big, it does not work:但是如果标题太大,则不起作用:

在此处输入图片说明

You should add following code for "3 dots" at the end.您应该在最后为“3 点”添加以下代码。

android:ellipsize="end"

You should remove fixed height property您应该删除固定高度属性

android:layout_height="50dip"

instead you should add number of lines相反,您应该添加行数

android:maxLines="4"  
android:layout_height="wrap_content"  

Android will take care everything else. Android 会处理其他一切。 In this way, even if text is smaller than 4 lines, android will take care size.这样,即使文本小于 4 行,android 也会照顾大小。 If it is more than 4 lines, it will add "3 dots" :) Specify fixed height may cut your text.如果超过 4 行,它将添加“3 个点”:) 指定固定高度可能会削减您的文本。

Try this.尝试这个。 Hope it will work.希望它会起作用。

<TextView
android:id="@+id/podcastShortDesc"
android:text="LONG LONG LONG TEXT"
android:layout_width="match_parent"
android:layout_height="50dip"
android:maxHeight="50dp"
android:ellipsize="end"/>

I have tested 50dp can show two line in normal font size.我已经测试过 50dp 可以以正常字体大小显示两行。 So in 50dp height you should add maxLines 2.所以在 50dp 高度你应该添加 maxLines 2。

Try this fix layout_height and add scroll in your textview试试这个修复 layout_height 并在你的 textview 中添加滚动

<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="50dip"
android:text="Hello......"
android:scrollbars="vertical"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM