gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:22:14 25 4
gpt4 key购买 nike

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

我试图了解如何使用 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 不受支持

此详细信息隐藏在开发人员指南的一行中: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).

要获得“匹配父级”行为,请将 0dp 维度与 View 的两个相应边缘的约束结合起来:

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 不会做任何事情,除非该 View 也有顶部约束,例如 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 的水平居中,我们将左右边缘约束到父级的左右边缘。这从每一侧均等地“拉动”AdView,使其居中。为了让它贴在屏幕底部,我们将它的底部边缘限制在父级的底部。

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

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

关于android - 如何将 AdView 放在 ConstraintLayout 的底部和其他东西下面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49137449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com