开发者

Textview show partial text

I have a TextView in which I must load a message. The TextView has maximum 2 lines (android:lines="2"). The message may contain an '\n' character.

How should I load the message in that TextView so that words would be wrapped and if in those 2 lines message does not fit, at the end of last visible word I must add three dots (...)? How can I detect the len开发者_如何学编程gth of text that fits in that TextView?

My code for TextView is

<TextView
            a:id="@+id/tv_message"
            a:gravity="top"
            a:layout_width="wrap_content"
            a:layout_height="wrap_content"
            a:layout_alignParentTop="true"
            a:layout_toRightOf="@id/iv_icon"
            a:layout_marginLeft="2dp"
            a:layout_marginTop="4dp"
            a:paddingRight="7dp"
            a:paddingBottom="5dp"
            a:textSize="12sp"
            a:typeface="sans"
            a:ellipsize="marquee"
            a:lines="2"
            a:maxLines="2"
            a:textColor="@android:color/black"
            />

But in application, text appears on two lines, even if there is a line containing the signature. The message is like: "message text" + "\nSignature" but the message can be on 1,2,3 lines, depending of message length.


In XML, use the property android:ellipsize="marquee"for the TextView.

Your TextView can be defined like:

<TextView 
android:id="@+id/text" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:maxLines="2" 
android:lines="2" 
android:ellipsize="marquee"/>

Edit: I've tried the following code, adapted from yours:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rlt_news"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/cell_background"
    android:padding="5dip" >

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:paddingLeft="5dip"
        android:text="Name"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/rss_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_title"
        android:layout_marginLeft="2dip"
        android:adjustViewBounds="true"
        android:maxHeight="100dip"
        android:maxWidth="100dip"
        android:src="@drawable/rss_cell_icon"
        android:visibility="visible" />

    <TextView
        android:id="@+id/tv_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="4dp"
        android:layout_toRightOf="@id/rss_icon"
        android:ellipsize="marquee"
        android:gravity="top"
        android:lines="2"
        android:maxLines="2"
        android:paddingBottom="5dp"
        android:paddingRight="7dp"
        android:text="Description of the news, just the short paragraph, \nSignature, to see how it works if there are more than two lines"
        android:textColor="@android:color/black"
        android:textSize="12sp"
        android:typeface="sans" />

</RelativeLayout>

And here is the output:

Textview show partial text


Use android:ellipsize="end" instead of "marquee" that did it for me.


Gabi,

I was also having the same problem. To enable ellipsize on a textview with 2 lines I use the following parameters:

android:maxLines="2"   // You already got this one
android:ellipsize="end" // also this one
android:singleLine="false"  // This is the command that solves your problem.

Unfortunately I got a new scenario for my app. I have to use 3 lines with ellipsize and this configuration only works for 2 lines... :( Good luck


If you set text from code, than you must set options once again!

textView.setText(content);
textView.setEllipsize(TruncateAt.END);
textView.setLines(5);
textView.setMaxLines(5);


It works for me when I set it in code not in the XML layout

holder.txtDescription.setText(text);
holder.txtDescription.setMaxLines(3);
holder.txtDescription.setEllipsize(TruncateAt.END);


    String s = "line1 \nline2 \nline3";
    String [] lines = s.split("\n");
    String twolines;
    if (lines.length > 2) {
        twolines = lines[0] + "\n" + lines[1] + "...";
    } else {
        twolines = s;
    }

This should give you the desired result, just use

((TextView)findViewById(R.id.text)).setText(twolines);


<TextView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:autoSizeTextType="uniform" />

android:autoSizeTextType="uniform" did the trick for me. Text size reduces to fit available space but complete text is displayed.

Refer this link https://developer.android.com/develop/ui/views/text-and-emoji/autosizing-textview

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜