【问题标题】:Android TextView with multiline and "android:ellipsize = middle"具有多行和“android:ellipsize = middle”的Android TextView
【发布时间】:2016-05-23 09:08:02
【问题描述】:
<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",文末有省略号。为什么?我怎么能在中间有省略号?

【问题讨论】:

  • 我不明白你想要什么,请添加屏幕截图
  • 你想把你的文字设置在中间吗?
  • 对于测试用例添加android:singleLine="true" 而不是android:maxLines="2"
  • 请在 Textview 中发布您想要的内容的截图?
  • @IntelliJAmiya 是的,我知道使用androidLsingleLine="true" 时,内容中间会有省略号。但我只想要带有多行和中间省略号的 TextView

标签: android textview


【解决方案1】:

android:ellipsize="middle" 似乎不适用于 Android 4.0 及更高版本上的多行。您可以尝试以下方法,但您只有一行。将以下内容添加到您的文本视图中。

android:singleLine="true"

【讨论】:

  • 哦,真的吗?“android:ellipsize="middle" 在 Android 4.0 及更高版本上似乎无法与多行一起工作。”是吗?
【解决方案2】:

试试这个代码:

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

这是截图

【讨论】:

    【解决方案3】:

    试试下面的代码,它的工作原理:

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

    【讨论】:

      【解决方案4】:

      如果 setMaxLines(int) 已用于设置两行或多行,则仅支持 TextUtils.TruncateAt.END 和 TextUtils.TruncateAt.MARQUEE (其他省略号类型不会执行任何操作)。 https://developer.android.com/reference/android/widget/TextView.html#attr_android:ellipsize

      如果你想使用 2 行的 textview 并且 ellipsize=middle 您可以将文本分成两半并使用它

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

      it looks like this

      【讨论】:

        【解决方案5】:

        试试这个,

             <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 文件中不允许使用特殊字符。

        【讨论】:

        • 不不,这不是我的问题...在屏幕截图中,当 TextView 具有确定的宽度并且内容将超过宽度时,我希望 TextView 中间有省略号。
        猜你喜欢
        • 2012-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-04
        • 2023-03-20
        • 2018-02-01
        • 1970-01-01
        相关资源
        最近更新 更多