【问题标题】:Why character / cuts the text in TextView if maxLines = 1?如果 maxLines = 1,为什么字符 / 剪切 TextView 中的文本?
【发布时间】:2016-05-31 15:37:21
【问题描述】:

我有什么

我有一个显示带有“https://”前缀的 URL 的 TextView:

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="test2"
            android:maxLines="1"
            android:ellipsize="marquee"
            android:textStyle="bold"
            android:textSize="16sp"
            android:textColor="@color/primary_text"
            android:id="@+id/txvNomEntornIversio" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="https://test2.sdasasdasdfasdfasdffasdfasdfasdf.sdffd"
            android:ellipsize="end"
            android:maxLines="1"
            android:paddingLeft="10dp"
            android:textSize="13sp"
            android:textStyle="italic"
            android:id="@+id/txvUrlEntorn"/>
        
    </LinearLayout>

有什么问题?

执行代码后在android studio预览和设备中的结果是:

如果执行之前的代码:

如果我删除“/”符号:

如果我将符号移动到其他位置:



我需要什么

我需要以最大字母显示前缀“https://”以适应可用空间。

我尝试了什么

我尝试read documentation (setEllipsize 块) 并在stackoverflow 中搜索,用这个符号搜索一些参考,但没有找到。 我也尝试对符号进行转义,但没有任何效果。

有人知道为什么会发生并可以帮助我吗?提前致谢!

更新布局

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="15dp"
    android:paddingBottom="15dp"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/icon_unchecked"
        android:layout_marginRight="10dp"
        android:id="@+id/txvIconChecked"/>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="test2"
            android:maxLines="1"
            android:ellipsize="marquee"
            android:textStyle="bold"
            android:textSize="16sp"
            android:textColor="@color/primary_text"
            android:id="@+id/txvNomEntornIversio" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="https://mydomain.asdfasdasdfasdfasdffasdfasdfasdf.cat"
            android:ellipsize="end"
            android:maxLines="1"
            android:paddingLeft="10dp"
            android:textSize="13sp"
            android:textStyle="italic"
            android:id="@+id/txvUrlEntorn"/>

    </LinearLayout>

</LinearLayout>

更新 2

感谢 Teiky,我们发现这个问题只出现在低于 API 23 的版本中。这可能是一个错误?

选项(已弃用)Android: SingleLine = "true" 似乎在所有版本中都能正常工作!相反,新选项 android: MaxLines with android: ellipsize 仅适用于 23 ...

目前,最可行的选择似乎是忽略已弃用的 SingleLine ...

我的解决方案

最后,正如我在上次更新中所说,我的解决方案是继续使用 android: SingleLine = true 即使它被标记为已弃用。

帝京秒,他尝试了一个dipsositiu android 4.4.4 并且使用MaxLines ellipsize 运行良好......而我,一个设备棒棒糖我仍然剪切文本以找到符号/......

作为回应 Teikyo,它帮助我进一步调查问题。谢谢大家的帮助!

【问题讨论】:

  • 在您的行中添加一个额外的 /:例如 https:/// // 中的第一个 / 是一个转义字符
  • 谢谢乔纳森,我尝试了这个:“https:///”,结果相同...还尝试了“https:\/\/”但没有效果...

标签: android view textview special-characters


【解决方案1】:

我尝试了您的代码,并且 URL 已完全显示。 如果您有足够的宽度来显示完整的 URL,也许您应该检查这些参数:

android:layout_width="0dp"
android:layout_weight="1"

xikitidisant 更新:

解决方案不是 weight 和 width="0dp"。这是一个代码改进。我的解决方案已在问题中更新。感谢您的帮助。

【讨论】:

  • 谢谢帝奇。你是对的,如果我的网址适合,这显示正确。但是我的胜利是当 URL 无法放入可用空间时显示最大 URL,在末尾添加三点...我用新视图更新了代码。
  • 哇,这是!我不明白为什么这对我不起作用....也许是因为 NavigationDrawer 左侧菜单中的一个项目具有固定的 240dp 宽度?让我尝试编译最后的更改...
  • 如果您查看 android studio 的预览视图,您也看对了吗?
  • 哇!如果您将目标更改为 API 23,它就像您的快照一样工作。尝试将其更改为低于 23 ......似乎这已解决到新的 API 23
  • 是的,你说得对。使用 API 22,预览会像您说的那样截断 URL。你试过模拟器吗?也许问题只是出现在预览中
【解决方案2】:
android:maxLines="1"
android:ellipsize="end"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-05
    • 2011-11-12
    • 1970-01-01
    相关资源
    最近更新 更多