Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

xml - Android TextView SingleLine field hides long text

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."

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"
    />
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is the purpose of "ellipsize" - truncating a portion of text to indicate additional text.

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:

This is really a really long...

(the 3 periods are the ellipsis - your text goes the opposite direction, but it should still work)

With a custom TextView, you could change the "..." to nothing or anything else you want (even an image, which I've done).

You could also marquee the text:

TextView Marquee not working


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...