防止在约束布局中受标签约束的单行TextView椭圆

问题描述 投票:0回答:2

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

这是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>

我想发生的约束是意识到文本将要溢出,而只能实现裕量直到文本不溢出为止。我确定我可以通过编程方式执行某些操作,但希望这是我可以在布局中实现的功能。

java android android-layout kotlin android-constraintlayout
2个回答
0
投票

testText TextView中删除此行:

app:layout_constraintStart_toStartOf="@id/testLabel"

并在textLabel TextView:]中添加此行

app:layout_constraintStart_toStartOf="@id/testText"

希望它可以根据需要运行。


0
投票

为什么不在顶部约束TextView的开始,而在底部约束TextView的开始?

© www.soinside.com 2019 - 2024. All rights reserved.