Android TextView SingleLine 字段隐藏长文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25247018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android TextView SingleLine field hides long text
提问by NFG
I have a TextView which sits on the left side of the screen and is set with gravity="right" and it is set with SingleLine = "true."
我有一个 TextView,它位于屏幕的左侧,设置为gravity="right",设置为 SingleLine="true"。
If by some chance the text in this view gets too long I want it to simply disappear off the left hand side of the view. I thought the configuration below would do that but what actually happens is the the long string disappears completely, presumably down and outside of the view somewhere.
如果这个视图中的文本太长,我希望它从视图的左侧消失。我认为下面的配置可以做到这一点,但实际发生的是长字符串完全消失了,大概是在视图下方的某个地方。
How can I create a simple text view that contains a single line of text and keeps its layout even when something unexpected happens? ... or even something predictable.
如何创建一个简单的文本视图,其中包含一行文本并在发生意外情况时保持其布局?......甚至是可预测的。
<TextView
android:id="@+id/tempF"
android:text="@string/tempF"
android:layout_width="146dp"
android:layout_height="wrap_content"
android:textColor="#cccccc"
android:textStyle="bold"
android:textSize="92dp"
android:fontFamily="serif"
android:gravity="right"
android:layout_marginTop="60dp"
android:layout_gravity="top|left"
android:singleLine="true"
/>
回答by Jim
This is the purpose of "ellipsize" - truncating a portion of text to indicate additional text.
这就是“ellipsize”的目的——截断文本的一部分以指示附加文本。
In you case, you may simply need to add something like:
在您的情况下,您可能只需要添加以下内容:
android:ellipsize="end"
or you might need to go deeper:
或者你可能需要更深入:
Automatically ellipsize a string in Java
Or look at this:
或者看看这个:
android ellipsize multiline textview
The idea behind extending the class is that you can customize the behavior of the TextView when the text content exceeds the space provided. For example, you can give it the appearance the it "bleeds over" by removing padding, etc. An ellipsis is an indicator that is commonly used to explain "there's more text that you can't see" - this is how it would look:
扩展类背后的想法是,当文本内容超出提供的空间时,您可以自定义 TextView 的行为。例如,您可以通过删除填充等来赋予它“流血”的外观。省略号是一个指示符,通常用于解释“还有更多您看不到的文本” - 这就是它的外观:
This is really a really long...
这真的是一个很长的...
(the 3 periods are the ellipsis - your text goes the opposite direction, but it should still work)
(这 3 个句点是省略号 - 你的文字走向相反的方向,但它应该仍然有效)
With a custom TextView, you could change the "..." to nothing or anything else you want (even an image, which I've done).
使用自定义 TextView,您可以将“...”更改为您想要的任何内容或任何其他内容(甚至是我已经完成的图像)。
You could also marquee the text:
您还可以选取文本:
回答by siddhartha shankar
Add 2 properties in xml
在xml中添加2个属性
android:singleLine="true"
android:ellipsize="end"
回答by rupesh jain
You should play with ellipsize attribute of the TextView.Check below:
您应该使用下面的 TextView.Check 的 ellipsize 属性:
<TextView
android:id="@+id/tempF"
android:text="@string/tempF"
android:layout_width="146dp"
android:layout_height="wrap_content"
android:textColor="#cccccc"
android:textStyle="bold"
android:textSize="92dp"
android:fontFamily="serif"
android:gravity="right"
android:layout_marginTop="60dp"
android:layout_gravity="top|left"
android:singleLine="true"
android:ellipsize="end"
/>
回答by Sunil
<TextView
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorGray"
android:textSize="@dimen/_18dp"
android:padding="@dimen/_2dp"
android:singleLine="true"
android:ellipsize="end"
android:text="@string/desc" />
回答by Hanisha
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000" />
It works.
有用。