即使未加载广告,广告主体也可见
Ad body visible even when ad is not loaded
我已经为我的应用制作了横幅广告。加载广告后,回收站视图保持不变,不会与其重叠,广告也不会与回收站视图重叠。但是,当广告未加载时,仍然存在与横幅尺寸相同的白色 'patch'。我想要的是,如果未加载广告,则该补丁不应存在于布局中,如果加载了广告,请创建一个类似的补丁,不要重叠回收器视图内容。我知道有一个用于重叠不同视图的框架布局,但据我所知,应用程序需要相对布局才能正常工作。
XML 文件
<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="#fafafa"
tools:context=".Activity.MainActivity">
<include
android:id="@+id/toolbarID"
layout="@layout/toolbar" />
<TextView
android:id="@+id/showWhenEmptyID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Add note"
android:textSize="22sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewID"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_below="@id/toolbarID"
android:layout_marginEnd="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_above="@id/adView"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<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"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
根据广告加载的响应,尝试在 java 代码中的 adView 上使用 VISIBILITY 模式。
或
只需在 xml 中将可见性设置为 GONE,然后根据 java 代码中加载广告的响应将其更改为 VISIBLE。
在你的xml中:
<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:visibility="gone" //SET VISIBILITY GONE
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
在你的activity中:
mAdView = findViewById(R.id.adView)
val adRequest = AdRequest.Builder().build()
mAdView.loadAd(adRequest)
mAdView.adListener = object : AdListener() { //ADDED AD LISTENER
override fun onAdLoaded() {
super.onAdLoaded()
adView.visibility = View.VISIBLE
}
}
我已经为我的应用制作了横幅广告。加载广告后,回收站视图保持不变,不会与其重叠,广告也不会与回收站视图重叠。但是,当广告未加载时,仍然存在与横幅尺寸相同的白色 'patch'。我想要的是,如果未加载广告,则该补丁不应存在于布局中,如果加载了广告,请创建一个类似的补丁,不要重叠回收器视图内容。我知道有一个用于重叠不同视图的框架布局,但据我所知,应用程序需要相对布局才能正常工作。
XML 文件
<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="#fafafa"
tools:context=".Activity.MainActivity">
<include
android:id="@+id/toolbarID"
layout="@layout/toolbar" />
<TextView
android:id="@+id/showWhenEmptyID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Add note"
android:textSize="22sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewID"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_below="@id/toolbarID"
android:layout_marginEnd="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_above="@id/adView"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<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"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
根据广告加载的响应,尝试在 java 代码中的 adView 上使用 VISIBILITY 模式。
或
只需在 xml 中将可见性设置为 GONE,然后根据 java 代码中加载广告的响应将其更改为 VISIBLE。
在你的xml中:
<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:visibility="gone" //SET VISIBILITY GONE
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
在你的activity中:
mAdView = findViewById(R.id.adView)
val adRequest = AdRequest.Builder().build()
mAdView.loadAd(adRequest)
mAdView.adListener = object : AdListener() { //ADDED AD LISTENER
override fun onAdLoaded() {
super.onAdLoaded()
adView.visibility = View.VISIBLE
}
}