【发布时间】: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