Как реализовать вертикальное сцепление с помощью Constraint Layout - PullRequest
0 голосов
/ 09 апреля 2020

У меня есть LinearLayout , каждое представление которого заполняет пространство, пропорциональное их значению веса.

У меня есть фрагмент layout_weight 8 и TextView layout_weight 2.

Это изображение ниже:

Linear Weight Image

С этим кодом:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    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"
    xmlns:tools="http://schemas.android.com/tools"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:orientation="vertical">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:padding="20dp"
        android:layout_weight="2"
        app:layout_constraintVertical_weight="2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:text="Lorem Ipsum is simply dummy text \nof the printing and typesetting industry" />

</LinearLayout>

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

Фрагмент заполняет экран, а TextView находится поверх него, а не просто занимает указанное пространство layout_constraintVertical_weight = 8 и layout_constraintVertical_weight = 2 соответственно.

Это изображение ниже:

enter image description here

С этим кодом ниже:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintVertical_chainStyle="spread"
        app:layout_constraintVertical_weight="8"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:padding="20dp"
        app:layout_constraintVertical_weight="2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:text="Lorem Ipsum is simply dummy text \nof the printing and typesetting industry" />

</androidx.constraintlayout.widget.ConstraintLayout>

В соответствии с документацией написано using the layout_constraintHorizontal_weight and layout_constraintVertical_weight attributes. If you're familiar with layout_weight in a linear layout, this works the same way. Это не работает для меня, или, может быть, я неправильно реализую.

Как я могу конвертировать мой LinearLayout в макет ограничения правильно?

Ответы [ 2 ]

1 голос
/ 09 апреля 2020

Для связывания видов необходимо связать их один с другим. В вашем xml вам нужно изменить фрагмент app:layout_constraintBottom_toBottomOf="parent" на app:layout_constraintBottom_toTopOf="@+id/textView2" и добавить app:layout_constraintTop_toBottomOf="@id/nav_host_fragment" в TextView.

Кроме того, я изменил android:layout_width="match_parent" на android:layout_width="0dp" как для фрагмента, так и для TextView, поскольку правильно использовать 0dp для расширения представлений между ограничениями.

Вот решение.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    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"
    xmlns:tools="http://schemas.android.com/tools"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:orientation="vertical">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@id/textView2"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_weight="8"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:padding="20dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/nav_host_fragment"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintVertical_weight="2"
        tools:text="Lorem Ipsum is simply dummy text \nof the printing and typesetting industry" />

</androidx.constraintlayout.widget.ConstraintLayout>
0 голосов
/ 09 апреля 2020

Для фрагмента вы используете неправильное нижнее ограничение. нижнее ограничение фрагмента должно быть верхом textview.

Кроме того, вам не нужно использовать layout_constraintVertical_chainStyle

enter image description here

    <?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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/textView2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_weight="8"
        app:navGraph="@navigation/nav_graph" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:padding="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintVertical_weight="2"
        tools:text="Lorem Ipsum is simply dummy text \nof the printing and typesetting industry" />

</androidx.constraintlayout.widget.ConstraintLayout>

2-й способ -> Кроме того, вы можете сделать это без веса. Без layout_constraintVertical_weight вы можете использовать guidline.

    <?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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.9" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:padding="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/guideline"
        tools:text="Lorem Ipsum is simply dummy text \nof the printing and typesetting industry" />

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