Android多行TextView和"android:ellipsize = middle"

5
<TextView
        android:id="@+id/food_item_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="bottom|center_horizontal"
        android:maxLines="2"
        android:ellipsize="middle"
        android:textSize="17sp"
        android:textColor="@color/food_cell_text"
        android:text=""Cured beef salad with rocket lettuce,cherry tomato & grana padano""/>

但是TextView中的文本没有省略号,就好像该属性无用一样。当我改变android:ellipsize="end"时,文本在末尾有了省略号。为什么?如何让文本中间也有省略号呢?


1
我无法理解您想要什么,请添加屏幕截图。 - Arpit Patel
你想把你的文本设置在中心吗? - Mehroz Munir
为测试用例添加 android:singleLine="true" 而不是 android:maxLines="2" - IntelliJ Amiya
请提供截图,您想在TextView中实现什么样的效果? - bhumika rijiya
1
@IntelliJAmiya 是的,我知道使用 androidLsingleLine="true" 时,文本内容会在中间出现省略号。但我只是希望TextView具有多行并在中间出现省略号。 - David.Cui
显示剩余2条评论
5个回答

2
在Android 4.0及以上版本中,android:ellipsize="middle"在多行文本中似乎不起作用。您可以尝试以下方法,它可以解决问题,但是只适用于单行文本。请将以下内容添加到您的textview中。
android:singleLine="true"

哦,真的吗?“android:ellipsize="middle"在Android 4.0及更高版本中似乎无法与多行一起使用。”是真的吗? - David.Cui
即使只有一行代码,在Android 11上也会出现错误。真遗憾像谷歌这样的公司无法解决这样的问题... - Slion

0

在此输入图片描述尝试使用以下代码:

  <TextView
    android:id="@+id/food_item_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/txt"
    android:layout_below="@+id/txt"
    android:layout_marginTop="64dp"
    android:ellipsize="middle"
    android:gravity="bottom|center_horizontal"
    android:maxLines="2"
    android:text="Cured beef salad with rocket lettuce,cherry tomato , grana padano"
    android:textColor="#000000"
    android:textSize="17sp" />

这是屏幕截图


0

尝试下面的代码,它是有效的:

<TextView
        android:id="@+id/food_item_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="bottom|center_horizontal"
        android:maxLines="1"
        android:ellipsize="middle"
        android:scrollHorizontally="true"
        android:textSize="17sp"
        android:textColor="@color/food_cell_text"
        android:text=""Cured beef salad with rocket lettuce,cherry tomato & grana padano""/>

0
如果使用setMaxLines(int)设置了两行或更多行,则仅支持TextUtils.TruncateAt.END和TextUtils.TruncateAt.MARQUEE(其他省略类型将不起作用)。 https://developer.android.com/reference/android/widget/TextView.html#attr_android:ellipsize 如果您想要使用带有2行和省略=middle的textview,您可以将文本分成两半并使用它。
      <TextView
            android:id="@+id/food_item_name_first_line"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="bottom|center_horizontal"
            android:singleLine="true"
            android:ellipsize="end"
            android:textSize="17sp"
            android:text="Cured beef salad with rocket "/>
       <TextView
            android:id="@+id/food_item_name_second_line"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="bottom|center_horizontal"
            android:singleLine="true"
            android:ellipsize="start"
            android:textSize="17sp"
            android:text="lettuce,cherry tomato & grana padano"/> 

它看起来像这样


-1

enter image description here

试试这个,

     <TextView
        android:id="@+id/food_item_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ellipsize="middle"
        android:layout_margin="50dp"
        android:padding="5dp"
        android:gravity="center"
        android:maxLines="2"
        android:textColor="@color/keyBagroung"
        android:text="@string/Curved"/>

尝试在Strings.xml文件中使用字符串, 像这样
 <string name="Curved">Cured beef salad with rocket lettuce,cherry tomato &amp; grana padano</string>

因为 XML 文件中不允许使用特殊字符。


2
不不,那不是我的问题...在屏幕截图中,当TextView具有确定的宽度且内容将超出宽度时,我希望TextView在中间具有省略号。 - David.Cui

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接