Android текстовое представление ограничения смежно / хвост к другому текстовому интерфейсу - PullRequest
1 голос
/ 05 марта 2020

Я некоторое время работал над макетом ограничений. Там требование, в котором я должен был ограничить textview1 к другому смежному textview2. Textview1 не имеет фиксированной длины, когда Textview1 увеличивается, он выталкивает мое textview2 из экрана

Это xml

<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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:ellipsize="end"
        android:maxLines="1"
        android:textColor="@color/colorPrimaryDark"
        android:text="this is long text this is long text this is long text this"
        app:layout_constraintEnd_toStartOf="@+id/textView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:maxLines="1"
        app:layout_constraintStart_toEndOf="@+id/textView2"
        app:layout_constraintTop_toTopOf="@id/textView2" />

</androidx.constraintlayout.widget.ConstraintLayout>

Вы также можете проверить скриншот для того же In this image the purple color is Textview1 and Textview2 is constraint to the endOf Textview1

Ответы [ 2 ]

1 голос
/ 06 марта 2020

Причина, по которой второй TextView выталкивается за пределы экрана, заключается в том, что представления с wrap_content по умолчанию не учитывают свои ограничения, когда для их удовлетворения недостаточно места. Вам необходимо явно установить app:layout_constrainedWidth="true" для первого TextView, чтобы обеспечить его ограничения в таком случае.

Во-вторых, чтобы иметь право придерживаться TextView слева, нужно поместить их в упакованную горизонтальную цепочку со смещением 0. Добавьте app:layout_constraintEnd_toEndOf="parent" ко второму TextView, чтобы создать действительную цепочку.

<?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"
        tools:context=".MainActivity">

    <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:textColor="@color/colorPrimaryDark"
            android:text="this is long text this is long text this is long text this"
            app:layout_constrainedWidth="true"
            app:layout_constraintHorizontal_chainStyle="packed"
            app:layout_constraintHorizontal_bias="0"
            app:layout_constraintEnd_toStartOf="@+id/textView"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:maxLines="1"
            app:layout_constraintStart_toEndOf="@+id/textView2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@id/textView2" />

</androidx.constraintlayout.widget.ConstraintLayout>
0 голосов
/ 05 марта 2020

Если вы хотите избежать выталкивания другого вида из экрана, не используйте android:layout_width="wrap_content".

Вы можете использовать app:layout_constraintWidth_percent="0.x", чтобы указать, что ваш вид равен некоторому проценту ширины экрана.

Например, поместите это в свой constraintLayout:

<TextView
    android:id="@+id/textView2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:ellipsize="end"
    android:maxLines="1"
    android:text="this is long text this is long text this is long text thisthis is long text this is long text this is long text thisthis is long text this is long text this is long text this"
    android:textColor="@color/colorPrimaryDark"
    app:layout_constraintEnd_toStartOf="@+id/textView"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintWidth_percent="0.5" />

<TextView
    android:id="@+id/textView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:text="TextView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/textView2"
    app:layout_constraintTop_toTopOf="@id/textView2" />

Это будет выглядеть так: enter image description here

И если вы хотите включить расширение textView, просто удалите android:maxLines="1"

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...