如果 maxLines = 1,为什么字符/剪切 TextView 中的文本?

Why character / cuts the text in TextView if maxLines = 1?

我有什么

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

    <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://”以适应可用 space。

我尝试了什么

我尝试 read documentation(setEllipsize 块)并在 Whosebug 中搜索,搜索带有此符号的一些参考,但没有找到。 我也尝试过对符号进行转义,但没有效果。

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

更新布局

<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,我们发现这个问题只出现在API23以下的版本,可能是bug?

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

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

我的解决方案

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

Teikyo 秒,他尝试了 dipsositiu android 4.4.4 并且使用 MaxLines ellipsize 效果很好......而我,一个设备 Lollipop 我仍然剪切文本以找到符号 / ...

设置响应 Teikyo 以及它帮助我进一步调查问题。谢谢大家的帮助!

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

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

xikitidistant 更新:

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

android:maxLines="1"
android:ellipsize="end"