как заставить линейное расположение в пределах ограничения полностью заполнить его область - PullRequest
0 голосов
/ 08 июля 2019

в приведенном ниже макете, я пытаюсь практиковать макет constarint.как указано в коде ниже, у меня есть Framelayouts, которые должны быть разделены по горизонтали.он также имеет фиолетовый фон.

Дело в том, что фиолетовый фон не полностью занимает среднюю часть.пожалуйста, дайте мне знать, как этого добиться, используя constarintlayout.

код :

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<LinearLayout
        android:id="@+id/topButtonsContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent">

    <Button
            android:id="@+id/topBtn_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="topBtn1"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toLeftOf="@id/topBtn_2"
            android:onClick="clicksHandler"/>

    <Button
            android:id="@+id/topBtn_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="topBtn2"
            app:layout_constraintLeft_toRightOf="@id/topBtn_1"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toLeftOf="@id/topBtn_3"
            android:onClick="clicksHandler"/>
    <Button
            android:id="@+id/topBtn_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="topBtn3"
            app:layout_constraintLeft_toRightOf="@id/topBtn_2"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:onClick="clicksHandler"/>

</LinearLayout>

<LinearLayout
    android:id="@+id/fragmentContainer"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:background="@android:color/holo_purple">

    <FrameLayout
        android:id="@+id/fragLeft"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">
    </FrameLayout>

    <FrameLayout
        android:id="@+id/fragRight"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">
    </FrameLayout>
</LinearLayout>

<LinearLayout
        android:id="@+id/LowerButtonsContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

    <Button
            android:id="@+id/bottomBtn_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BottomBtn1"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@id/bottomBtn_2"
            app:layout_constraintBottom_toBottomOf="parent"
            android:onClick="clicksHandler"/>

    <Button
            android:id="@+id/bottomBtn_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BottomBtn2"
            app:layout_constraintLeft_toRightOf="@id/bottomBtn_1"
            app:layout_constraintRight_toLeftOf="@id/bottomBtn_3"
            app:layout_constraintBottom_toBottomOf="parent"
            android:onClick="clicksHandler"/>
    <Button
            android:id="@+id/bottomBtn_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BottomBtn3"
            app:layout_constraintLeft_toRightOf="@id/bottomBtn_2"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:onClick="clicksHandler"/>

</LinearLayout>

</android.support.constraint.ConstraintLayout>

1 Ответ

0 голосов
/ 08 июля 2019

Ваш вопрос был не так ясен, вы что-то сказали о textViews, но в вашем макете их 0, вот мой ответ на основе того, что я понял из вопроса:


Если вам нужно простое решение, просто чтобы максимизировать область действия фрагмента фрагмента, просто измените его высоту на 0dp и ограничьте верхнюю часть фрагмента фрагмента нижней частью верхней части, а нижнюю часть фрагмента фрагмента - верхней.ваша нижняя часть выглядит так:

<LinearLayout
    android:id="@+id/fragmentContainer"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:background="#f88813"
    app:layout_constraintBottom_toTopOf="@+id/LowerButtonsContainer"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/topButtonsContainer">

    <FrameLayout
        android:id="@+id/fragLeft"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">
    </FrameLayout>

    <FrameLayout
        android:id="@+id/fragRight"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">
    </FrameLayout>
</LinearLayout>

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

enter image description here


Для лучшего решения и лучшего понимания ConstraintLayout:

В вашем макете имеется множество вложенных представлений (ConstraintLayout-> LinearLayout-> FrameLayout).

Макет Constraint не предназначендля вложенных групп представлений цель ConstraintLayout - иметь иерархию плоских представлений.

Из документации :

ConstraintLayoutпозволяет создавать большие и сложные макеты с плоской иерархией представлений (без вложенных групп представлений).Он похож на RelativeLayout в том, что все представления расположены в соответствии с отношениями между родственными представлениями и родительским макетом, но он более гибкий, чем RelativeLayout, и его проще использовать с редактором макетов Android Studio.

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

Такпримерно так:

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


<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:text="Buttons"
    app:layout_constraintBottom_toBottomOf="@+id/button4"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/button4"
    app:layout_constraintTop_toTopOf="@+id/button4" />

<Button
    android:id="@+id/button5"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:text="we are"
    app:layout_constraintBottom_toBottomOf="@+id/button6"
    app:layout_constraintEnd_toStartOf="@+id/button6"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/button6" />

<Button
    android:id="@+id/button6"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="top"
    app:layout_constraintEnd_toStartOf="@+id/button7"
    app:layout_constraintStart_toEndOf="@+id/button5"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button7"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:text="Buttons"
    app:layout_constraintBottom_toBottomOf="@+id/button6"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/button6"
    app:layout_constraintTop_toTopOf="@+id/button6" />

<Button
    android:id="@+id/button3"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:text="we are"
    app:layout_constraintBottom_toBottomOf="@+id/button4"
    app:layout_constraintEnd_toStartOf="@+id/button4"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/button4" />

<Button
    android:id="@+id/button4"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:text="bottom"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/button2"
    app:layout_constraintStart_toEndOf="@+id/button3" />

<Button
    android:id="@+id/button8"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:text="Hey I am blue"
    android:background="#ff123456"
    app:layout_constraintBottom_toTopOf="@+id/button4"
    app:layout_constraintEnd_toStartOf="@+id/button9"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button6" />

<Button
    android:id="@+id/button9"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:text="And i am orange "
    android:background="#f88813"
    app:layout_constraintBottom_toTopOf="@+id/button4"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/button8"
    app:layout_constraintTop_toBottomOf="@+id/button6" />
</android.support.constraint.ConstraintLayout>

И это будет выглядеть так:

enter image description here


Обратите внимание, что в моем более подробном примере у меня 0 вложенных групп представлений, поэтому мой макет будет работать быстрее.

...