小编Luc*_*zzi的帖子

如何使用 ConstraintLayout 水平对齐三个 TextView

我试图水平对齐三个 TextViews(正如标题所暗示的那样),但即使将它们链接在一起并使用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>
Run Code Online (Sandbox Code Playgroud)

这些是使用示例布局的一些情况

没有一个 TextViews 是椭圆化的:

第一种情况

第一个 TextView 正在椭圆化:

第二种情况

最后一个 TextView 不会椭圆化并将其他的推出屏幕:

第三种情况


这些是一些可能的解决方案,并未涵盖所有情况 …

android android-constraintlayout

5
推荐指数
1
解决办法
2651
查看次数

标签 统计

android ×1

android-constraintlayout ×1