Проблема ConstraintLayout: некоторые кнопки невидимы - PullRequest
0 голосов
/ 07 февраля 2019

Я все еще пытаюсь понять ConstraintLayout.

Допустим, у меня есть эта деятельность:

enter image description here

Который состоитRecyclerView и 3 Buttons.RecyclerView может содержать много items.Эти 3 Buttons остаются в нижней части экрана.

Код:

<?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="fill_parent"
    tools:context=".ScanResultActivity">

    <android.support.v7.widget.RecyclerView
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/btn_buy_more"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:text="Buy More"
        app:layout_constraintBottom_toBottomOf="@id/recycler_view" />

    <Button
        android:id="@+id/btn_checkout"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:text="Checkout"
        app:layout_constraintBottom_toBottomOf="@id/btn_buy_more" />

    <Button
        android:id="@+id/btn_logout"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:text="Logout"
        app:layout_constraintBottom_toBottomOf="@id/btn_checkout" />


</android.support.constraint.ConstraintLayout>

И результат:

enter image description here

Почему там видна только 1 кнопка?

Ответы [ 3 ]

0 голосов
/ 07 февраля 2019

Установите Buttons в LinearLayout

Как в следующем коде

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

    <android.support.v7.widget.RecyclerView
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="@+id/recycler_view">

        <Button
            android:id="@+id/btn_buy_more"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:text="Buy More"
            app:layout_constraintBottom_toBottomOf="@id/recycler_view"/>

        <Button
            android:id="@+id/btn_checkout"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:text="Checkout"
            app:layout_constraintBottom_toBottomOf="@id/btn_buy_more"/>

        <Button
            android:id="@+id/btn_logout"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:text="Logout"
            app:layout_constraintBottom_toBottomOf="@id/btn_checkout"/>

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

Я надеюсь, что это работает ....

0 голосов
/ 07 февраля 2019

Видна только одна кнопка, потому что она выше других, так как она настроена на выравнивание Снизу на низ .

Если вы хотите установить одну кнопку над другой, вам следует использовать Снизу наверх ограничение.

Я не знаю, какой именно порядок вы хотите установить, но это должно быть примерно так:

    <android.support.v7.widget.RecyclerView
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/btn_buy_more"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:text="Buy More"
        app:layout_constraintBottom_toBottomOf="@id/recycler_view" />

    <Button
        android:id="@+id/btn_checkout"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:text="Checkout"
        app:layout_constraintBottom_toTopOf="@id/btn_buy_more" />

    <Button
        android:id="@+id/btn_logout"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:text="Logout"
        app:layout_constraintBottom_toTopOf="@id/btn_checkout" />

</android.support.constraint.ConstraintLayout>
0 голосов
/ 07 февраля 2019

Первая проблема, высота вашего RecyclerView была установлена ​​на match_parent, и она не была ограничена верхней частью представления.

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

Переделан файл макета:

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

    <android.support.v7.widget.RecyclerView
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0"
        app:layout_constraintBottom_toTopOf="@id/btn_buy_more"/>

    <Button
        android:id="@+id/btn_buy_more"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:text="Buy More"
        app:layout_constraintBottom_toTopOf="@id/btn_checkout" />

    <Button
        android:id="@+id/btn_checkout"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:text="Checkout"
        app:layout_constraintBottom_toTopOf="@id/btn_logout" />

    <Button
        android:id="@+id/btn_logout"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:text="Logout"
        app:layout_constraintBottom_toBottomOf="parent"/>


</android.support.constraint.ConstraintLayout>

Результат:

enter image description here

...