Android: как отобразить виджет поверх другого с помощью ConstraintLayout? - PullRequest
0 голосов
/ 14 сентября 2018

Я использую ConstraintLayout, и у меня есть три виджета: View (это простая строка, которая соответствует ширине родительского элемента и имеет высоту 1dp), и два FloatingActionButton.Эти два должны отображаться поверх View и центрироваться вертикально с последним.Я буду использовать поля, чтобы отделить их.Мой вопрос: как отобразить виджет поверх другого , используя ConstraintLayout ?

Ответы [ 2 ]

0 голосов
/ 15 сентября 2018

, поскольку grrigore уже отлично ответил на вопрос, я хочу отметить:

Последовательность тегов виджетов в файлах макета XML важна, последний будет перекрывать предыдущий, чтобы избежать этого,Вы можете добавить android:background атрибут к предыдущему виджету, чтобы обозначить это как фон.

0 голосов
/ 15 сентября 2018

Надеюсь, я понял ваш вопрос. Вы можете использовать FrameLayout или ConstraintLayout решение.

Это мое решение с использованием FrameLayout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_gravity="center"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:background="@color/colorPrimary" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|start"
        android:layout_margin="16dp"
        android:src="@drawable/ic_launcher_foreground" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|end"
        android:layout_margin="16dp"
        android:src="@drawable/ic_launcher_foreground" />

</FrameLayout>

Результат:

result

Это мое решение с использованием ConstraintLayout:

<?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:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteY="81dp">

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:layout_gravity="center"
        android:layout_margin="8dp"
        android:background="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|start"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="84dp"
        android:layout_marginStart="84dp"
        android:layout_marginTop="8dp"
        android:src="@drawable/ic_launcher_foreground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.5" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|end"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="84dp"
        android:layout_marginRight="84dp"
        android:layout_marginTop="8dp"
        android:src="@drawable/ic_launcher_foreground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.5" />

</android.support.constraint.ConstraintLayout>

Результат:

result

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

Не забудьте добавить implementation 'com.android.support:design:27.1.1' в файл build.gradle(Module:app).

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