【问题标题】:Textview show partial textTextview 显示部分文本
【发布时间】:2011-06-19 02:28:30
【问题描述】:

我有一个 TextView,我必须在其中加载一条消息。 TextView 最多有 2 行 (android:lines="2")。该消息可能包含“\n”字符。

我应该如何在该 TextView 中加载消息以便包装单词,如果这两行消息不适合,在最后一个可见单词的末尾我必须添加三个点 (...)?如何检测适合该 TextView 的文本长度?

我的 TextView 代码是

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

但在应用程序中,文本出现在两行上,即使有一行包含签名。消息类似于:“消息文本”+“\nSignature”,但消息可以是 1、2、3 行,具体取决于消息长度。

【问题讨论】:

  • 为什么需要知道文本的长度?除非您为 TextView 定义了固定宽度,否则它可能会因不同的屏幕尺寸而异。
  • 我的问题是我不知道如何在最后一个可见单词的末尾添加 3 个点。 android:ellipsize="end" 和 android:ellipsize="marquee" 没有帮助....所以我认为我不会将所有文本添加到 textview,而只添加适合 textview 的文本
  • 您可以发布您现在使用的 TextView 的代码吗?也许还有其他属性会阻止椭圆形的工作。

标签: android android-layout textview word-wrap


【解决方案1】:

在 XML 中,为 TextView 使用属性 android:ellipsize="marquee"

你的 TextView 可以这样定义:

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

编辑:我尝试了以下代码,改编自您的:

<?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>

这是输出:

【讨论】:

  • 它没有帮助...如果我有消息:line1 \nline2 \nline3 在 line2 之后我必须添加 3 个点,因为消息不适合
  • 您是否尝试使用此属性?它只是截断第二行之后的文本并为你放置三个点,我从我的代码中剪切并粘贴它。
  • 我已经用你的代码替换了我的代码,它可以工作,我发布了一张带有结果的图片...我不知道那里发生了什么,也许你也在代码中设置了一些属性?或者只是我不明白你想要什么..你也试过@slup提供的新解决方案吗?
  • 是的,我正在从代码中设置文本...这可能是个问题吗?
  • 不,我也在设置代码中的文本;这里我只是把它放在 XML 中,让你看看我用了什么文本,但我对代码中的文本有同样的效果。但我认为除了文本之外,您可能还通过代码设置了其他属性。
【解决方案2】:

使用 android:ellipsize="end" 而不是为我做的“字幕”。

【讨论】:

    【解决方案3】:

    加比,

    我也遇到了同样的问题。要在 2 行的 textview 上启用 ellipsize,我使用以下参数:

    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.
    

    不幸的是,我的应用有了新的场景。我必须使用 3 行和 ellipsize 并且此配置仅适用于 2 行... :( 祝你好运

    【讨论】:

    • @Gasper 它对我有用。我申请1行。非常感谢。
    【解决方案4】:

    如果您从代码中设置文本,则必须再次设置选项!

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

    【讨论】:

      【解决方案5】:

      当我在代码中设置它而不是在 XML 布局中时,它对我有用

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

      【讨论】:

        【解决方案6】:
            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;
            }
        

        这应该会给你想要的结果,只需使用

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

        【讨论】:

        • 它没有帮助...如果我有消息:line1 \nline2 \nline3 在 line2 之后我必须添加 3 个点,因为消息不适合
        • 但是消息的第一行可能足够长,可以容纳 3 行 textview,所以这无济于事
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-19
        • 1970-01-01
        • 1970-01-01
        • 2014-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多