Android: 如何使用 ConstraintLayout 将一个小部件显示在另一个小部件之上?
Android: How to display a widget over another one using ConstraintLayout?
我使用 ConstraintLayout
并且我有三个小部件:一个 View
(这是一条简单的线,在宽度上与父级匹配,高度为 1dp),两个 FloatingActionButton
.这两个必须显示在 View
上方并与后者垂直居中。我将使用边距将它们分开。我的问题是:如何使用 ConstraintLayout 在另一个小部件上显示一个小部件 ?
希望我理解你的问题。您可以使用 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>
结果:
这是我使用 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>
结果:
您可以根据需要使用不同的 marings/padding。
不要忘记将 implementation 'com.android.support:design:27.1.1'
添加到您的 build.gradle(Module:app)
文件中。
由于grrigore已经完美地回答了问题,我想指出:
widgets标签在layout xml文件中的顺序很重要,后一个会和前一个重叠,为了避免这种情况,你可以在prior中添加android:background
属性表示这是背景的小部件。
我使用 ConstraintLayout
并且我有三个小部件:一个 View
(这是一条简单的线,在宽度上与父级匹配,高度为 1dp),两个 FloatingActionButton
.这两个必须显示在 View
上方并与后者垂直居中。我将使用边距将它们分开。我的问题是:如何使用 ConstraintLayout 在另一个小部件上显示一个小部件 ?
希望我理解你的问题。您可以使用 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>
结果:
这是我使用 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>
结果:
您可以根据需要使用不同的 marings/padding。
不要忘记将 implementation 'com.android.support:design:27.1.1'
添加到您的 build.gradle(Module:app)
文件中。
由于grrigore已经完美地回答了问题,我想指出:
widgets标签在layout xml文件中的顺序很重要,后一个会和前一个重叠,为了避免这种情况,你可以在prior中添加android:background
属性表示这是背景的小部件。