无法在ConstraintLayout中将Admob Banner广告设置在页面底部的问题
我帮你搞定这个布局问题!你现在的情况是Banner广告跑到ActionBar上方去了,完全没按预期待在底部对吧?咱们调整一下布局的约束关系就能解决,以下是具体方案:
核心思路
问题出在控件的约束设置上,咱们需要明确每个组件的位置关系:顶部ActionBar → 中间WebView占满剩余空间 → 底部Banner固定在屏幕底部。用ConstraintLayout来实现这个布局是最灵活的,下面是修改后的完整布局代码:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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"> <!-- 中间的WebView,占据ActionBar下方到Banner上方的所有空间 --> <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@+id/adView" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> <!-- 底部的Admob Banner,固定在屏幕底部 --> <com.google.android.gms.ads.AdView android:id="@+id/adView" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:adSize="BANNER" app:adUnitId="@string/admob_banner_id" /> </androidx.constraintlayout.widget.ConstraintLayout>
关键细节说明
- WebView的
layout_height设为0dp,配合constraintTop和constraintBottom的约束,让它自动填充系统ActionBar(会自动占据顶部空间)到Banner之间的所有可用空间。 - AdView的
constraintBottom_toBottomOf="parent"确保它牢牢固定在屏幕底部,同时constraintTop_toBottomOf="@+id/webView"让它刚好接在WebView的下方,不会重叠。
如果使用自定义Toolbar的情况
要是你用自定义的Toolbar代替系统ActionBar,只需要把Toolbar加到布局最顶部,然后调整WebView的顶部约束到Toolbar的底部即可:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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"> <!-- 自定义Toolbar --> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> <!-- WebView现在顶部对齐Toolbar底部 --> <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintTop_toBottomOf="@+id/toolbar" app:layout_constraintBottom_toTopOf="@+id/adView" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" /> <!-- 底部Banner不变 --> <com.google.android.gms.ads.AdView android:id="@+id/adView" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:adSize="BANNER" app:adUnitId="@string/admob_banner_id" /> </androidx.constraintlayout.widget.ConstraintLayout>
这样调整后,布局就会严格按照你想要的顺序排列:顶部ActionBar/Toolbar,中间WebView占满空间,底部Banner固定在屏幕最下方,再也不会覆盖ActionBar啦!
内容的提问来源于stack exchange,提问作者Shahbaz Alam
相关产品推荐
相关产品推荐

