Фиксированная часть цепи в ConstraintLayout - PullRequest
0 голосов
/ 27 апреля 2018

С помощью цепочки ConstraintLayout мы можем расположить несколько элементов так, чтобы они были расположены равномерно. Чтобы усилить боль, каждый вид имеет свой размер ...:

|~~~~~AA~~~~~B~~~~~CCCCC~~~~~DD~~~~~|

Теперь я хотел бы, чтобы часть этой цепочки была исправлена, а другая часть цепочки адаптировалась в результате:

|~~~~~~AA~~~~~~B-CCCCC~~~~~~DD~~~~~~|
                  ^
                center

Возможно ли без добавления видов в цепочку и сохранения ширины каждого вида в wrap_content?

Ответы [ 2 ]

0 голосов
/ 27 апреля 2018

Мы можем построить цепочку между любыми двумя видами, а также по сторонам родителя. Вот первый пример с созданием цепочки для всех четырех представлений. «AA» и «DD» ограничены начальной и конечной стороной родительского элемента:

enter image description here

Все представления находятся в цепочке, и члены цепочки равномерно распределены по родительскому элементу.

Вот пример, где "CCCCC" центрируется на директиве, которая сама центрирована в родителе «AA» и «BB» теперь находятся в собственной цепочке и равномерно распределены по промежутку между началом родительского элемента и началом «CCCCC». «DD» центрируется в своем пространстве, но не участвует в цепочке, потому что цепочке нужно как минимум два члена. «CCCCC» не находится в цепочке, но имеет «фиксированное» положение в горизонтальном центре макета.

enter image description here

Наконец, вот пример, где начало "CCCCC" ограничено центральной линией. Как вы можете видеть, все другие виды корректируются в соответствии с их ограничениями при перемещении «CCCCC».

enter image description here

После правильного ограничения представления будут корректировать свои позиции в соответствии с правилами ограничения, когда «CCCCC» перемещается по макету. См. "Цепочки" для официальной документации.

0 голосов
/ 27 апреля 2018
<?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">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:text="TextView"
        app:layout_constraintEnd_toStartOf="@+id/textView2"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="TextView"
        app:layout_constraintEnd_toStartOf="@+id/guideline"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="16dp"
        android:text="TextView"
        app:layout_constraintEnd_toStartOf="@+id/textView4"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/guideline"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="16dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/textView3"
        app:layout_constraintTop_toTopOf="parent"/>

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

enter image description here

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