【问题标题】:Prevent Single Line TextView Ellipses constrained by Label in Constraint Layout防止在约束布局中受标签约束的单行 TextView 椭圆
【发布时间】:2020-03-06 13:19:39
【问题描述】:

我有两个垂直对齐的 TextView,顶部 TextView 作为底部 TextView 的标签。我们可以称它们为 testLabel 和 testText。为了使它们保持左对齐,testText 具有 start -> startOf 到 testLabel 的约束。这些 TextView 位于父级的右上角。 testLabel 对具有边距值的父级有一个 end -> endOf 约束。问题是标签的长度可以变化,当标签的长度足够小的值时,标签将根据与父级的边距和约束设置正确更新其位置,但这也会导致文本对齐其开始。那导致文本离开屏幕,如果文本足够长,则显示一个省略号。我可以说文本的长度是佩里非常恒定的。我创建了一个简单的 Activity 来说明问题。 以下是标签不够小且一切正常的情况: 当标签足够小时:

这是xml:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/testLabel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="32dp"
        android:layout_marginRight="32dp"
        android:text="@android:string/ok"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/testText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="6dp"
        android:layout_marginRight="6dp"
        android:gravity="start"
        android:singleLine="true"
        android:text="MonkeyBars"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@id/testLabel"
        app:layout_constraintTop_toBottomOf="@id/testLabel" />

</androidx.constraintlayout.widget.ConstraintLayout>

我想要发生的是约束意识到文本将溢出,而是只满足文本不溢出的边距。我确信我可以通过编程方式做一些事情,但希望这是我可以在布局中实现的事情。

【问题讨论】:

  • 你能展示一下你想要什么行为吗?
  • 我会更新信息,但本质上我希望标签和 textview 保持左对齐,没有任何一个文本导致省略号,并且标签是唯一长度可以变化的 textview
  • 那么,如果两个视图都是宽视图的宽度,它会起作用吗?

标签: java android android-layout kotlin android-constraintlayout


【解决方案1】:

testText TextView 中移除这一行:

app:layout_constraintStart_toStartOf="@id/testLabel"

并在 textLabel TextView 中添加这一行:

app:layout_constraintStart_toStartOf="@id/testText"

希望它能如你所愿。

【讨论】:

    【解决方案2】:

    为什么不将顶部的 TextView 的开头约束到底部的 TextView 的开头?

    <TextView
            android:id="@+id/testLabel"
            app:layout_constraintStart_toStartOf="@+id/testText"
            ... />
    

    这里是完整的代码:

    <?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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <TextView
            android:id="@+id/testLabel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="32dp"
            android:text="@android:string/ok"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@+id/testText"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/testText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="6dp"
            android:gravity="start"
            android:singleLine="true"
            android:text="MonkeyBars"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@id/testLabel" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
    

    【讨论】:

    • 所以问题是标签是可变长度的。因此,如果标签的文本足够长,则会导致标签出现椭圆。现在我可以做的是让文本的边距足够大,以覆盖标签的最大长度。
    • 您不需要增加标签的边距。只需将标签的开始限制为最长的 TextView 的开始。
    【解决方案3】:

    如果您希望两个文本视图都左对齐并且标签的长度可以变化,那么试试这个

    <?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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <TextView
            android:id="@+id/testLabel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="32dp"
            android:layout_marginRight="32dp"
            android:text="@android:string/ok"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/testText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="start"
            android:singleLine="true"
            android:text="MonkeyBars"
            app:layout_constraintEnd_toEndOf="@id/testLabel"
            app:layout_constraintTop_toBottomOf="@id/testLabel" />
    

    【讨论】:

      猜你喜欢
      • 2018-03-15
      • 1970-01-01
      • 2018-12-12
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      • 2022-11-02
      • 2022-10-18
      • 1970-01-01
      相关资源
      最近更新 更多