如何将 AdView 放在 ConstraintLayout 的底部和其他东西下面?

How to put AdView in ConstraintLayout at bottom and below other stuff?

我正在尝试从 RelativeLayout 迁移到 ConstraintLayout,但我在将 AdMob AdView 添加到底部以及将其余内容添加到其上方时遇到问题。例如,使用 RelativeLayout 就是这么简单。您只需将 android:layout_alignParentBottom="true" 放在 AdView 上,将 android:layout_above="@+id/adView" 放在包含内容的视图上。

我正在尝试了解如何使用 ConstraintLayout 而不是 RelativeLayout 将此示例代码和这两行代码迁移到等效代码。

请问有人可以帮助我完成此迁移吗?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@android:color/transparent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/adView"/>

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="5dp"
        ads:adSize="BANNER"
        ads:adUnitId="@string/ad_id_banner">
    </com.google.android.gms.ads.AdView>
</RelativeLayout>

我测试了在 adView 上方我想要的内容上使用 app:layout_constraintBottom_toTopOf="@+id/adView" 并在 adView 上使用 app:layout_constraintBottom_toBottomOf="parent" 但它不起作用,因为这些内容不在它后面的 adView 上方。而且 adView 也没有居中。很郁闷。

首先,我想谈谈 ConstraintLayout 独有的两个行为,当你第一次接触它时,它们不一定很明显。

不支持

match_parent

此详细信息隐藏在开发人员指南的 one-liner 中:https://developer.android.com/training/constraint-layout/index.html

Note: You cannot use match_parent for any view in a ConstraintLayout. Instead use "match constraints" (0dp).

要获得 "match parent" 行为,请将 0dp 维度与视图的两个对应边的约束相结合:

android:layout_width="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"

边距只有在有相应约束的情况下才会强制执行

仅添加 android:layout_marginTop 不会执行任何操作,除非该视图也具有顶部约束,例如 app:layout_constraintTop_toTopOfapp:layout_constraintTop_toBottomOf.

您的具体问题

这是更新后的布局:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="5dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/adView"/>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:adSize="BANNER"
        app:adUnitId="@string/ad_id_banner"/>

</android.support.constraint.ConstraintLayout>

为了实现AdView的水平居中,我们将左右边缘约束到父级的左右边缘。这 "pulls" AdView 从每一边均匀地居中。为了让它粘在屏幕底部,我们将它的底部边缘限制在父级的底部。

至于LinearLayout,首先我们把它的维度都改成0dp。这将使约束定义其大小。然后我们将上边缘、左边缘和右边缘约束到父对象的上边缘、左边缘和右边缘。我们将底边限制为 AdView 的顶边。这会导致它水平填充屏幕,并填充 space AdView 未使用的所有垂直屏幕。

最后,我们将 AdView 的上边距更改为 LinearLayout 的下边距。这是因为 LinearLayout 的底边被限制在 AdView 的顶边(但是 AdView 的顶边没有被限制在 LinearLayout' s 底部边缘,因此顶部边距不会有任何影响)。