如何将AdView放在ConstraintLayout的底部和下方?

Nul*_*ion 13 android android-layout android-relativelayout android-constraintlayout

我正在尝试从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>
Run Code Online (Sandbox Code Playgroud)

app:layout_constraintBottom_toTopOf="@+id/adView"在adView上面和adView app:layout_constraintBottom_toBottomOf="parent"上测试了我想要的东西,但它不起作用,因为这些东西不在它后面的adView之上.此外,adView不会居中.这非常令人沮丧.

Ben*_* P. 21

首先,我想谈谈两个独特的行为,ConstraintLayout当你第一次拿起它时,这些行为并不一定是显而易见的.

match_parent 不受支持

这个细节隐藏在开发者指南中的单行内容中:https://developer.android.com/training/constraint-layout/index.html

注意:您不能match_parent用于任何视图ConstraintLayout.而是使用"匹配约束"(0dp).

要获得"匹配父级"行为,请将0dp维度与约束对应边缘的约束组合在一起:

android:layout_width="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
Run Code Online (Sandbox Code Playgroud)
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
Run Code Online (Sandbox Code Playgroud)

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

只是添加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>
Run Code Online (Sandbox Code Playgroud)

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

至于LinearLayout,首先我们改变它的尺寸0dp.这将使约束定义其大小.然后我们将顶部,左侧和右侧边缘约束到父级的顶部,左侧和右侧边缘.我们将底边限制在顶边AdView.这会导致它水平填充屏幕,并填充所有AdView未使用的垂直空间.

最后,我们将上边距更改AdView为下边距LinearLayout.这是因为它LinearLayout的下边缘被约束到了AdView顶边(但是AdView上边缘没有约束LinearLayout到底边,所以上边距没有效果).