How to proper align three TextViews horizontally with ConstraintLayout

问题: I'm trying to align three TextViews horizontally (as the title may suggest), but even after chaining they together and using app:layout_constrainedWidth="true" they're stil...

问题:

I'm trying to align three TextViews horizontally (as the title may suggest), but even after chaining they together and using app:layout_constrainedWidth="true" they're still being pushed out of the screen, strangely only by the left side when the middle or the last one grows large. Notice that I'm setting the android:maxLines="1" and android:ellipsize="end" and aligning they at the start of the screen, not sure if it matters though.

I also tried to limit their sizes, but on the case where there are two small texts and a very large one the large one will be ellipsized even when the layout still have some space left.

Sample layout:

<?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>

These are some cases using the sample layout

None of the TextViews are ellipsizing:

First case

The first TextView is ellipsizing:

Second case

The last TextView don't ellipsize and pushes the other ones out of the screen:

Third case


These are some possible solutions that aren't covering all of the cases

Using android:maxWidth="90dp" to prevent the TextView to grow, but also limiting the text unnecessarily:

Fourth case

Using android:layout_width="0dp" to enable "match_constraint" is not a optimal solution as well, since the layout will reserve a third of the display width for each TextView, even if the text is smaller than that:

Fifth case


回答1:

Can you try this one (just make sure to replace androidx.constraintlayout.widget.ConstraintLayout with android.support.constraint.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 will work as expected.

enter image description here


回答2:

You can use linear layout instead. When the text grows it will ellipsized end with ...

<?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>

OUTPUT:

enter image description here

  • 发表于 2019-02-22 14:30
  • 阅读 ( 740 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除