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

标签 android view textview special-characters

我有什么

我有一个显示带有“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 预览和设备中的结果是:

如果执行前面的代码: enter image description here

如果我删除“/”符号: enter image description here

如果我将符号移动到其他位置: enter image description here



我需要什么

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

我尝试了什么

我试过 read documentation (setEllipsize block )并在 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>

enter image description here

enter image description here

更新 2

感谢Teiky,我们发现这个问题只出现在API 23以下的版本,可能是bug?

选项(已弃用)Android: SingleLine = "true"似乎在所有版本中都能正常工作!相反,新选项 android: MaxLines with android: ellipsize 仅适用于 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 更新:

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

关于android - 如果 maxLines = 1,为什么字符/剪切 TextView 中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37550024/

相关文章:

android - 自定义 arrayadapter 组合了在同一 Activity 的不同 fragment 中使用的列表

android - 如何使用 android studio 3.0 设置 GCE 开发服务器

android - PhoneGap(Android)支持不同分辨率

android - 移动动画时变换 ImageView(保持比例)

php - 有可能在变量中获取 Phalcon\Mvc\View 渲染输出吗?

android - 如何设置TextView的textStyle如粗体、斜体

android - 如何在工具栏的右侧放置 ProgressBar?

javascript - 警告 : Each child in an array or iterator should have a unique "key" prop. 检查 'search' 的渲染方法

android - 如何设置部分 TextView 可点击

android - Textview 在 setRetainInstance 中不起作用