【问题标题】:Why are words that go outside a single line TextView's bounds not rendered and clipped?为什么超出单行 TextView 边界的单词不会被渲染和剪切?
【发布时间】:2021-10-14 09:56:25
【问题描述】:

我有一个 TextView 包含在这样的容器中:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="40dp"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:maxLines="1"
        android:text="Hi World" />

</FrameLayout>

FrameLayout 上 40 dp 的宽度不足以包含整行文本。因此,我希望 TextView 被其父级剪辑。相反,TextView 根本不呈现“世界”这个词!

Expected Actual
  1. 为什么第二个词没有呈现?
  2. 我怎样才能让TextView 被剪裁呢?这是宽度更改动画(将容器调整为 0dp 宽度)的一部分,其中删除字母或 文本块 会导致烦人的闪烁。

【问题讨论】:

    标签: android android-layout textview android-animation


    【解决方案1】:

    这正如你所料

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
    
            android:singleLine="true"
            android:ellipsize="none"
    
            android:maxLines="1"
            android:text="Hi World" />
    

    还有这个

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
    
            android:inputType="text"
    
            android:maxLines="1"
            android:text="Hi World" />
    

    【讨论】:

    • 我认为 singleLine 已被弃用,但也许这只适用于 EditTexts (stackoverflow.com/questions/30879471/…)。明天试试这个。我对使用inputType 有点犹豫,因为文档明确指出“将此属性设置为除无外的任何内容也意味着文本是可编辑的”。
    • 因为是 TextView 而不是 EditText 它不适用
    • 经过测试并且可以正常工作。 AS 确实在建议框中将singleLine 标记为已弃用,但在写出后不会。
    猜你喜欢
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    相关资源
    最近更新 更多