Макет ограничения не ограничивает вложенные элементы других макетов - PullRequest
0 голосов
/ 24 октября 2018

Я пытаюсь создать макет списка, который начинается в нижней части кнопки и имеет 3/4 ширины экрана.

Точно так же:

imagine that there is a button instead of a bar

Сначала я использовал вложенные линейные макеты по горизонтали и вертикали, но потом я прочитал здесь , что макеты ограничений более отзывчивы, и я был впечатлен и попробовал.

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

мой код выглядит следующим образом:

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


    <ImageButton
        android:id="@+id/settings_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="#FFFFFF"
        android:src="@drawable/ic_menu_black_32dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <LinearLayout
        android:id="@+id/list_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="4"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/settings_button"
        app:layout_constraintTop_toBottomOf="@+id/settings_button">

        <ListView
            android:id="@+id/listview"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

Мой текущий макет выглядит следующим образом:

current layout

Я действительно ценю помощников!

Ответы [ 2 ]

0 голосов
/ 24 октября 2018

Вам нужно изменить высоту разметки линейной разметки на 0dp, я здесь выложу раскладку.Также рассмотрите возможность перехода на androidx:)

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


<ImageButton
    android:id="@+id/settings_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgroundTint="#FFFFFF"
    android:src="@drawable/ic_menu_black_32dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

<LinearLayout
    android:id="@+id/list_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:weightSum="4"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/settings_button"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/settings_button">

    <ListView
        android:id="@+id/listview"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3" />
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
0 голосов
/ 24 октября 2018

Я думаю, что использование match_parent в качестве ширины или высоты дочернего элемента и добавление каких-либо ограничений здесь неправильно.Отредактировано 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=".MainActivity">


<ImageButton
    android:id="@+id/settings_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="24dp"
    android:layout_marginTop="24dp"
    android:backgroundTint="#FFFFFF"
    android:src="@drawable/ic_menu_black_32dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

<LinearLayout
    android:id="@+id/list_linear_layout"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="24dp"
    app:layout_constrainedWidth="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="@+id/settings_button"
    app:layout_constraintTop_toBottomOf="@+id/settings_button"
    app:layout_constraintWidth_percent="0.75">

    <ListView
        android:id="@+id/listview"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"/>
</LinearLayout>

...