【发布时间】:2019-02-20 13:29:29
【问题描述】:
我正在尝试水平对齐三个 TextView(如标题所示),但即使在将它们链接在一起并使用 app:layout_constrainedWidth="true" 之后,它们仍然被推出屏幕,奇怪的是,当中间或最后一个变大。请注意,我正在设置 android:maxLines="1" 和 android:ellipsize="end" 并将它们对齐在屏幕的开头,但不确定它是否重要。
我也尝试限制它们的大小,但是在有两个小文本和一个非常大的文本的情况下,即使布局仍有一些空间,大文本也会被省略。
示例布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:background="@android:color/white"
android:padding="22dp">
<TextView
android:id="@+id/greenTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@android:color/holo_green_dark"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toStartOf="@id/blueTextView"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="TextView" />
<TextView
android:id="@+id/blueTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@android:color/holo_blue_dark"
app:layout_constrainedWidth="true"
app:layout_constraintBaseline_toBaselineOf="@id/greenTextView"
app:layout_constraintEnd_toStartOf="@id/orangetextView"
app:layout_constraintStart_toEndOf="@id/greenTextView"
tools:text="Another TextView " />
<TextView
android:id="@+id/orangetextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@android:color/holo_orange_dark"
app:layout_constrainedWidth="true"
app:layout_constraintBaseline_toBaselineOf="@id/blueTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/blueTextView"
tools:text="Another TextView" />
</android.support.constraint.ConstraintLayout>
这些是使用示例布局的一些案例
没有一个 TextView 是椭圆的:
第一个TextView是椭圆化的:
最后一个 TextView 没有椭圆化并将其他的推出屏幕:
这些是一些可能的解决方案,并未涵盖所有情况
使用android:maxWidth="90dp"来阻止TextView增长,同时也不必要地限制了文本:
使用android:layout_width="0dp" 启用“match_constraint”也不是最佳解决方案,因为布局将为每个 TextView 保留三分之一的显示宽度,即使文本小于该宽度:
【问题讨论】:
-
用ConstraintLayout可能达不到你想要的效果。如果它适用于 Flexbox,那么您可以使用它。
标签: android android-constraintlayout