【问题标题】:How to proper align three TextViews horizontally with ConstraintLayout如何使用 ConstraintLayout 正确水平对齐三个 TextView
【发布时间】: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


【解决方案1】:

使用弹性盒

<com.google.android.flexbox.FlexboxLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:alignContent="flex_start"
        app:alignItems="flex_start"
        app:flexWrap="nowrap"
        app:dividerDrawable="@drawable/ic_divider"
        app:showDivider="middle">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:textSize="18sp"
            app:layout_minWidth="50dp"
            android:textColor="@android:color/holo_green_dark"
            android:ellipsize="end"
            android:text="Text View"/>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:ellipsize="end"
            android:textColor="@android:color/holo_blue_dark"
            android:textSize="18sp"
            app:layout_minWidth="50dp"
            android:text="Another TextView"/>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:textColor="@android:color/holo_orange_dark"
            android:textSize="18sp"
            app:layout_minWidth="50dp"
            android:ellipsize="end"
            android:text="Another TextView"/>

</com.google.android.flexbox.FlexboxLayout>

此布局的输出:

【讨论】:

    【解决方案2】:

    您可以改用线性布局。当文本增长时,它会以椭圆结尾...

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        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="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginEnd="8dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:textColor="@android:color/holo_green_dark"
            tools:text="TextView" />
    
        <TextView
            android:id="@+id/blueTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginEnd="8dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:textColor="@android:color/holo_blue_dark"
            tools:text="Another TextView " />
    
        <TextView
            android:id="@+id/orangetextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginEnd="8dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:textColor="@android:color/holo_orange_dark"
            tools:text="Another TextView" />
    </LinearLayout>
    

    输出:

    【讨论】:

    • app:layout_constraint*** LinearLayout 不需要
    • 抱歉放错了那个东西。 :)
    • 感谢您的回答,但这将 TextViews 的大小限制为屏幕总宽度的三分之一,例如:如果第一个文本很小,布局仍将保留相同的空间不必要的。
    • 在这种情况下,您可以使用FlexboxLayout,当第一个TextView 到达右侧时,它会自动将下一个TextView 移动到新行。如果您愿意,我可以使用FlexboxLayout 编辑上述答案。
    • 这个想法是省略文本并将它们三个都放在一行中,我也尝试使用 flexbox 但没有成功,如果您知道如何做到这一点,那将是一个很大的帮助。也只是出于好奇的目的,我想知道是否可以仅使用 ConstraintLayout 来做到这一点。
    【解决方案3】:

    你可以试试这个(只要确保用 android.support.constraint.ConstraintLayout 替换 androidx.constraintlayout.widget.ConstraintLayout):

    <?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"
        android:background="@android:color/white"
        android:padding="22dp">
    
        <TextView
            android:id="@+id/greenTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:textColor="@android:color/holo_green_dark"
            app:layout_constraintEnd_toStartOf="@id/blueTextView"
            app:layout_constraintHorizontal_chainStyle="packed"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="First Very Long Text 123456789" />
    
        <TextView
            android:id="@+id/blueTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:textColor="@android:color/holo_blue_dark"
            app:layout_constraintBaseline_toBaselineOf="@id/greenTextView"
            app:layout_constraintEnd_toStartOf="@id/orangetextView"
            app:layout_constraintStart_toEndOf="@id/greenTextView"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="Second Very Long Text 123456789" />
    
        <TextView
            android:id="@+id/orangetextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:textColor="@android:color/holo_orange_dark"
            app:layout_constraintBaseline_toBaselineOf="@id/blueTextView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/blueTextView"
            tools:text="Third Very Long Text 123456789" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    Ellipsize 将按预期工作。

    【讨论】:

    • 感谢您的回答,但这会为每个 TextView 保留三分之一的宽度,类似于在线性布局上使用权重,因此如果第一个文本很小,它将保留一个额外的空间用于在其他两个上显示更多文本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 2017-03-07
    • 2019-09-30
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    相关资源
    最近更新 更多