2

В моем ConstraintLayout два виджета: EditText и TextView. TextView копирует содержимое EditText.

Мне нужно, чтобы ширина EditText ограничивалась первой половиной экрана, а ширина TextView - правой границей EditText и концом экрана. Т.е. при вводе содержимого в EditText TextView cдвигался бы вправо, пока не занял бы вторую половину экрана.

введите сюда описание изображения

Для этого я устанавливаю guideline на 50% ширины экрана и использую ее как правый ограничитель для EditText. А ограничители для TextView - это правый край EditText слева и конец экрана справа. Для обоих виджетов ширину устанавливаю wrap_content, но с учетом ограничителей - устанавливаю true на layout_constrainedWidth.

XML

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

<EditText
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    android:inputType="textNoSuggestions"
    android:minWidth="1dp"
    android:scrollHorizontally="true"
    app:layout_constrainedWidth="true"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/guideline"
    app:layout_constraintTop_toTopOf="parent"/>

<HorizontalScrollView
    android:id="@+id/horizontalScrollView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constrainedWidth="true"
    app:layout_constraintBottom_toBottomOf="@+id/editText"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toRightOf="@+id/editText"
    app:layout_constraintRight_toRightOf="parent">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:maxLines="1"
        android:scrollHorizontally="true"
        android:singleLine="true"
        app:layout_constrainedWidth="true"
        app:layout_constraintBaseline_toBaselineOf="@+id/editText"
        app:layout_constraintEnd_toStartOf="@+id/textView"
        app:layout_constraintStart_toEndOf="@+id/editText"/>
</HorizontalScrollView>

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.5"/>

</android.support.constraint.ConstraintLayout>

На выходе получаю: TextView доходит до конца экрана и начинает вытеснять EditText влево, не принимая в расчет левый ограничитель.

введите сюда описание изображения

Неправильно использую layout_constrainedWidth или что-то еще упускаю из виду?

1 ответ 1

1

Неправильно использую layout_constrainedWidth

Да и это тоже. Попробуйте пример ниже, в теории должно быть так, как вы хотите.

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:inputType="textNoSuggestions"
        android:minWidth="1dp"
        android:scrollHorizontally="true"
        app:layout_constrainedWidth="true"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginStart="16dp" />

    <HorizontalScrollView
        android:id="@+id/horizontalScrollView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="@+id/editText"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toRightOf="@+id/editText"
        app:layout_constraintRight_toRightOf="parent">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:maxLines="1"
            android:scrollHorizontally="true"
            android:singleLine="true" />
    </HorizontalScrollView>

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5" />
</android.support.constraint.ConstraintLayout>
3
  • С match_constraint все работает (первую гифку с ним и делал). А если мне понадобится добавить правее TextView еще один виджет, который должен двигаться вместе с ним при вводе текста, а потом ограничивать его с правого края? Почему match_constraint учитывает оставшееся место и ограничители, а wrap_content с constrainedWidth нет? 1 июл 2018 в 7:25
  • сделайте отступ справа не от родителя а от это вью, а эта вью справа от родителя. У вас явно не корректное понимание параметра match_constraint , как ещё дать ответ на ваш вопрос, если всё работает как вы хотите ? 1 июл 2018 в 7:30
  • но тогда этот вью будет "прибит" к правому краю, а не будет следовать за TextView в самом начале 1 июл 2018 в 7:51

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.