Android 显示AdMob横幅时调整布局
我正在我的Android 显示AdMob横幅时调整布局,android,layout,admob,Android,Layout,Admob,我正在我的活动中集成AdMob横幅广告。布局由GridView和AdView组成 我想要的:- <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:ads="http://schemas.
活动中集成AdMob横幅广告
。布局由GridView
和AdView
组成
我想要的:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10"
android:id="@+id/ll_gridmain"
tools:context="mohits.app.picreddit.itemgrid">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
android:divider="@null"
android:numColumns="2"
android:background="#454545" >
</GridView>
<LinearLayout android:id="@+id/banner_view"
android:orientation="horizontal"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:visibility="gone">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="match_parent"
ads:adSize="BANNER"
ads:adUnitId="@string/ad_banner"
android:visibility="gone">
</com.google.android.gms.ads.AdView>
</LinearLayout>
</LinearLayout>
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("xxx")
.build();
mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
mAdView.setVisibility(View.VISIBLE);
bannerView.setVisibility(View.VISIBLE);
}
});
当广告未显示时,即开始或下载广告时,我希望GridView
具有所有屏幕。但当广告被显示时,它应该覆盖下面所需的空间,但不要重叠任何内容
我的布局:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10"
android:id="@+id/ll_gridmain"
tools:context="mohits.app.picreddit.itemgrid">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
android:divider="@null"
android:numColumns="2"
android:background="#454545" >
</GridView>
<LinearLayout android:id="@+id/banner_view"
android:orientation="horizontal"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:visibility="gone">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="match_parent"
ads:adSize="BANNER"
ads:adUnitId="@string/ad_banner"
android:visibility="gone">
</com.google.android.gms.ads.AdView>
</LinearLayout>
</LinearLayout>
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("xxx")
.build();
mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
mAdView.setVisibility(View.VISIBLE);
bannerView.setVisibility(View.VISIBLE);
}
});
我得到了什么:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10"
android:id="@+id/ll_gridmain"
tools:context="mohits.app.picreddit.itemgrid">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
android:divider="@null"
android:numColumns="2"
android:background="#454545" >
</GridView>
<LinearLayout android:id="@+id/banner_view"
android:orientation="horizontal"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:visibility="gone">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="match_parent"
ads:adSize="BANNER"
ads:adUnitId="@string/ad_banner"
android:visibility="gone">
</com.google.android.gms.ads.AdView>
</LinearLayout>
</LinearLayout>
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("xxx")
.build();
mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
mAdView.setVisibility(View.VISIBLE);
bannerView.setVisibility(View.VISIBLE);
}
});
banner\u视图
布局始终显示,即使我将其设置为GONE
。只有在显示广告时,此选项才可见
谢谢。问题是,即使您删除了横幅,
GridView
的权重仍然是9
,布局的weightSum
是10
,因此GridView
不会填充整个线性布局
从布局中删除以下行应该有效:
android:weightSum="10"
此外,设置AdView
的可见性也是多余的
设置视图组的可见性也会影响其子对象
您可以从AdView
中删除以下属性:
android:visibility="gone"
之后,设置横幅视图的可见性就足够了:
@Override
public void onAdLoaded() {
super.onAdLoaded();
bannerView.setVisibility(View.VISIBLE);
}