【问题标题】:Android Layout: How to ellipsize TextView depending on surrounding textviews?Android 布局:如何根据周围的文本视图来省略文本视图?
【发布时间】:2020-04-03 20:02:21
【问题描述】:

我需要您的帮助来设计一个符合以下几个要求的布局:

要求:

  • TextA(绿色):

    1. 一直都在。
    2. 长度可能不同(1 到 50 个字符)。
    3. 是唯一一个在内容太长时必须省略空间的选项。
  • TextB(黄色):

    1. 可以为空。 (宽度:0dp)
    2. 长度可能不同(0 到 10 个字符)。
    3. 如果不为空,则必须完全可见(不是椭圆/截断)
    4. 必须贴在 TextA 的右侧。
  • TextC(红色):

    1. 一直都在。
    2. 具有固定宽度(即:100dp)。
    3. 始终坚持在父项的右侧。

注意:TextATextBTextC 是 TextView。

我的问题与this question 类似,但有 3 个部分而不是 2 个部分,要求略有不同。

有人知道如何实现吗?

谢谢

【问题讨论】:

    标签: android layout textview android-wrap-content ellipsize


    【解决方案1】:

    您的问题已回答here

    但该解决方案将使您的文本视图具有相同的宽度。 请记住使用android:layout_weight="1" 仅用于中间textview

    【讨论】:

    • 谢谢,但这似乎不是我正在寻找的解决方案...我需要 TextA 和 TextB 的宽度灵活(即:wrap_content)和 TextC 的宽度固定@ 100dp。
    • 当你设置android:layout_weight="1"时,最后一个textview会粘在最后,如果中间的textview到达最后,它会回到椭圆模式
    • 如果我猜对了,TextB(或TextC)会在某个时候变成椭圆形吗?因为我只需要将 TextA 椭圆化;另外两个绝对不能。
    【解决方案2】:

    您无法在 xml 中实现 100%,因为您需要有条件地将 TextB 上的可见性设置为 View.GONE。但是,使用 ConstraintLayout 您可以完成其余的行为,如下所示: layout preview

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <TextView
        android:id="@+id/TextA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:text="TextAaekfnvauefoviqwejnoiwjeofijnweoimivejmgpiwvmpgr"
        android:ellipsize="end"
        android:maxLines="1"
        android:textColor="@color/green"
        app:layout_constraintEnd_toStartOf="@+id/TextC"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
        android:id="@+id/TextC"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="TextC"
        android:textColor="@color/red"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
        android:id="@+id/TextB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:text="TextB"
        android:textColor="@color/yellow"
        app:layout_constraintEnd_toStartOf="@+id/TextC"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/TextA"
        app:layout_constraintTop_toTopOf="parent" />
    

    【讨论】:

    • 有趣... :-) 我可以使用 DataBinding 为 TextB 设置条件可见性。你认为它会起作用吗?
    • 我不熟悉数据绑定库,但似乎可以
    • 我刚刚尝试了您的示例代码,它完全可以工作(即使 TextB 可见性为“Gone”)。没有文本被椭圆化,并且 TextA 最终位于 TextC 之上。 :-(你能告诉我“textView6”指的是什么吗?如果问得不多,你能给我一个完整的(除了TextB可见性问题)功能布局示例吗?:-)
    • @IceMatter 抱歉,我在复制代码时一定错过了其中一个 ID。现在更新了
    • 感谢更新,但它仍然不适用于长 TextA;它与 TextC 重叠... :-(
    猜你喜欢
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    • 2011-03-30
    • 2020-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多