java阻止约束布局中受标签约束的单行TextView省略号
我有两个文本视图,它们与作为底部文本视图标签的顶部文本视图垂直对齐。我们可以称之为testLabel和testText。为了使它们保持左对齐,testText有start->;开始测试标签。这些文本视图位于父视图的右上角。testLabel有一个端点->;使用边距值将约束赋给父级。问题在于,标签的长度可能会有所不同,当标签的长度值足够小时,标签将根据父标签的边距和约束设置正确更新其位置,但这也会导致文本对齐其起点。这会导致文本离开屏幕,如果文本足够长,就会显示一个省略号。我可以说文本的长度是非常恒定的。我创建了一个简单的活动来说明这个问题。 当标签不够小,而且看起来一切正常时,情况如下: 当标签足够小时:
以下是xml:
<?xml version="1.0" encoding="utf-8"?>
<安卓x.constraintlayout.widget.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
安卓:id="@+id/testLabel"
安卓:layout_width="0dp"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="24dp"
安卓:layout_marginEnd="32dp"
安卓:layout_marginRight="32dp"
安卓:text="@安卓:string/ok"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
安卓:id="@+id/testText"
安卓:layout_width="0dp"
安卓:layout_height="wrap_content"
安卓:layout_marginEnd="6dp"
安卓:layout_marginRight="6dp"
安卓:gravity="start"
安卓:singleLine="true"
安卓:text="MonkeyBars"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/testLabel"
app:layout_constraintTop_toBottomOf="@id/testLabel" />
</安卓x.constraintlayout.widget.ConstraintLayout>
我希望发生的是,约束意识到文本将溢出,而只满足文本不会溢出的限度。我确信我可以通过编程来做一些事情,但希望这是我可以在布局中实现的事情
# 1 楼答案
如果你想让两个文本视图都左对齐,并且标签的长度可以不同,那么试试这个
# 2 楼答案
从测试文本中删除这一行:
并在textLabelTextView中添加这一行:
希望它能如你所愿