Android Ellipsize 不适用于自定义 listView 中的 textView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1424276/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:04:16  来源:igfitidea点击:

Ellipsize not working for textView inside custom listView

xmlandroidtextview

提问by aspartame

I have a listView with custom objects defined by the xml-layout below. I want the textView with id "info" to be ellipsized on a single line, and I've tried using the attributes

我有一个 listView,其中包含由下面的 xml-layout 定义的自定义对象。我希望将 ID 为“info”的 textView 在一行上进行省略,并且我尝试使用这些属性

android:singleLine="true"
android:ellipsize="end"

without success.

没有成功。

If I set the layout_width to a fixed width like e.g.

如果我将 layout_width 设置为固定宽度,例如

android:layout_width="100px"

the text is truncated fine. But for portability reasons this is not an acceptable solution.

文本被截断得很好。但出于便携性原因,这不是一个可接受的解决方案。

Can you spot the problem?

你能发现问题吗?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5px"
>
<TextView  
android:id="@+id/destination"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:textSize="22dp"
android:paddingLeft="5px"
/>

<TextView  
android:id="@+id/date"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:textSize="15dp"
android:paddingLeft="5px"
/>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/info_table"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="5px"
android:paddingTop="10px" 
>
    <TableRow>
        <TextView
            android:id="@+id/driver_label"
            android:gravity="right"
            android:paddingRight="5px"
            android:text="@string/driver_label" />
        <TextView
            android:id="@+id/driver" />
    </TableRow>
    <TableRow>
        <TextView
            android:id="@+id/passenger_label"
            android:gravity="right"
            android:paddingRight="5px"
            android:text="@string/passenger_label" />
        <TextView
            android:id="@+id/passengers" />
    </TableRow>
    <TableRow>
        <TextView
            android:id="@+id/info_label"
            android:gravity="right"
            android:paddingRight="5px" 
            android:text="@string/info_label"/>
        <TextView
            android:id="@+id/info"
            android:layout_width="fill_parent"
            android:singleLine="true"
            android:ellipsize="end" />
    </TableRow>
</TableLayout> 

采纳答案by Jeremy Logan

Ellipsize is broken (go vote on the bug report, especially since they claim it's not reproducible) so you have to use a minor hack. Use:

Ellipsize 坏了(对错误报告进行投票,特别是因为他们声称它不可重现)所以你必须使用一个小技巧。用:

android:inputType="text"
android:maxLines="1"

on anything you want to ellipsize. Also, don't use singleLine, it's been deprecated.

在任何你想要椭圆化的地方。另外,不要使用singleLine,它已被弃用。

UPDATE:

更新:

On closer inspection, the problem you're having is that your table is extending off the right side of the screen. Changing your TableLayoutdefinition to:

仔细检查后,您遇到的问题是您的桌子超出了屏幕的右侧。将您的TableLayout定义更改为:

<TableLayout
    android:id="@+id/info_table"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="5px"
    android:paddingTop="10px"
    android:shrinkColumns="1">

should fix that problem, then do what I said above to ellipsize your TextView.

应该解决该问题,然后按照我上面所说的将您的TextView.

回答by tbruyelle

Without using deprecated properties, the following lines work great for me

不使用已弃用的属性,以下几行对我很有用

android:ellipsize="end" 
android:lines="1"
android:scrollHorizontally="true"

ref http://code.google.com/p/android/issues/detail?id=882#c9

参考http://code.google.com/p/android/issues/detail?id=882#c9

回答by Andy Cochrane

I had the same results as Steve:

我得到了与史蒂夫相同的结果:

android:ellipsize="marquee" android:scrollHorizontally="true" android:lines="1"

android:ellipsize="marquee" android:scrollHorizo​​ntally="true" android:lines="1"

works perfectly

完美运行

回答by hwii77

try...

尝试...

i.e.

IE

android:ellipsize="marquee"
android:focusable="true" android:marqueeRepeatLimit="num|marquee_forever"
android:lines="1" android:focusableInTouchMode="true"
android:scrollHorizontally="true"

as marquee only works when view is selected or focused according to the comment#2 on http://code.google.com/p/android/issues/detail?id=5364

因为选取框仅在根据http://code.google.com/p/android/issues/detail?id=5364上的评论 #2 选择或聚焦视图时才有效

回答by Robert

After checking out these 3 issues(closed & re-opened) and previous posts, I think the key is to wrap up TextView with a outer LinearLayout(or any other layout you are using), and set the "layout_width" of outer layout width a little bit SMALLERthan the text length contained in the TextView. Seems has to LIMITthe width to make the "android:ellipsize" behave properly.

在检查了这 3 个问题(关闭并重新打开)和以前的帖子后,我认为关键是用外部 LinearLayout(或您正在使用的任何其他布局)包装 TextView,并设置外部布局宽度的“ layout_width”一点点SMALLER,不包含在TextView的文本长度。似乎必须限制宽度才能使“android:ellipsize”正常运行。

Follow the tip in this issue, after adding all these attributes for hacking, I also set the layout_width of outer LinearLayout to 240dp for example:

按照本期提示,在添加所有这些属性后,我还将外部 LinearLayout 的 layout_width 设置为 240dp 例如:

<Linearlayout
  android:layout_width="240dp"
  android:layout_height="wrap_content"
  ...>

    <TextView
     ....
     android:singleLine="true"
     android:lines="1"
     android:maxLines="1"
     android:scrollHorizontally="true"
     android:ellipsize="end"
     android:text="this is a text which length should be longer than 240dp"
    />
</LinearLayout>

Than I can successfully make the end of TextView displaying "...", not very acceptable but it works! And I think it works whether used in regular TextView or a TextView in a List Adapter.

比我可以成功地使 TextView 结束显示“...”,不是很可接受,但它有效!而且我认为无论是在常规 TextView 还是 List Adapter 中的 TextView 中使用,它都有效。

Here's my screenshot for the result!

这是我的结果截图!

hope it helps ^^

希望有帮助^^

Ref of those 3 issues:

参考这 3 个问题:

issue 882

第 882 期

issue 10554

问题 10554

issue 31229

问题 31229

回答by Shan Xeeshi

For me these attribute worked. I tested on API 8 and onwards

对我来说,这些属性有效。我在 API 8 及以后的版本上进行了测试

In XML

在 XML 中

<TextView
 ....
 android:maxLines="1"
 android:scrollHorizontally="true"
 android:ellipsize="end"
 ....
/>

Programmatically

以编程方式

setMaxLines(1);
setEllipsize(TruncateAt.END);
setHorizontallyScrolling(true);

回答by Helin Wang

If you put TextView inside a custom ViewGroup you created, make sure to measure it again if you changed the dimension after the measurement.

如果您将 TextView 放在您创建的自定义 ViewGroup 中,如果您在测量后更改了尺寸,请确保再次测量它。

E.g., I calculated the new dimension in onLayout. Before you call child.layout, call

例如,我在 onLayout 中计算了新维度。在调用 child.layout 之前,调用

measureChild(child,
    View.MeasureSpec.makeMeasureSpec(childWidth, View.MeasureSpec.EXACTLY),
    View.MeasureSpec.makeMeasureSpec(childHeight, View.MeasureSpec.EXACTLY));

回答by Shri

I had the same problem in recycler view card. I have resolved this by adding following code:

我在回收商视图卡中遇到了同样的问题。我通过添加以下代码解决了这个问题:

    <TextView
        android:ellipsize="end"
        android:maxLines="1"/>

回答by molokoloco

I found somewhere this attributes

我在某处找到了这个属性

<TextView ...
android:singleLine="true"
android:ellipsize="marquee" />

it work fine for me (Android 1.5)

它对我来说很好用(Android 1.5)