【问题标题】:Admob Banner & Smart Banner ads fills entire screen on loadAdmob 横幅和智能横幅广告在加载时会填满整个屏幕
【发布时间】:2021-06-29 05:15:52
【问题描述】:

我在我的 Android 应用中使用 Smart_Banner 和 BANNER 广告。

当广告首次加载或广告更改时,会出现覆盖整个屏幕的闪光灯。这对用户来说是非常烦人的。这是什么原因,我该如何解决?

我的实现基于 Fragments。所有相关代码如下所示。

implementation 'com.google.android.gms:play-services-ads:19.1.0'
implementation 'com.google.android.gms:play-services-auth:18.0.0'
implementation 'com.google.android.gms:play-services-identity:17.0.0'
implementation 'com.google.android.gms:play-services-games:19.0.0'
implementation 'com.google.android.material:material:1.1.0'

添加片段

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/backcolor"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="@string/BANNERAD_UNIT_ID">
</com.google.android.gms.ads.AdView>
  public class AdFragment extends Fragment {

    private AdView mAdView;

    public AdFragment() {
    }

    @Override
    public void onActivityCreated(Bundle bundle) {
        super.onActivityCreated(bundle);

        try {
            mAdView = (AdView) getView().findViewById(R.id.adView);
            
            AdRequest adRequest = new AdRequest.Builder()
                   .build();
            mAdView.loadAd(adRequest);
        }catch (Exception e)
        {
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.adfragment, container, false);
    }

    /** Called when leaving the activity */
    @Override
    public void onPause() {
        if (mAdView != null) {
            mAdView.pause();
        }
        super.onPause();
    }

    /** Called when returning to the activity */
    @Override
    public void onResume() {
        super.onResume();
        if (mAdView != null) {
            mAdView.resume();
        }
    }

    /** Called before the activity is destroyed */
    @Override
    public void onDestroy() {
        if (mAdView != null) {
            mAdView.destroy();
        }
        super.onDestroy();
    }

}

主布局

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/fragment_fake"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_above="@+id/fragment"
        android:gravity="center"
        android:orientation="vertical" />

    <LinearLayout
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/fragment_ad"
        android:gravity="center"
        android:orientation="vertical" />

    <fragment
        android:id="@+id/fragment_ad"
        class="com.controllers.AdFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

【问题讨论】:

  • 是在广告之后不小心关闭的标签:adUnitId="@string/BANNERAD_UNIT_ID">
  • 能不能也贴一下膨胀片段的代码
  • 最后显示的主布局代码在主活动上膨胀。没有代码膨胀它。

标签: android admob fragment ads


【解决方案1】:

我添加了第二行来设置布局高度。这解决了我的问题。 我不确定这是否是最好的解决方案,但它似乎至少可以在我的手机上运行。

  mAdView = (AdView) getView().findViewById(R.id.adView);
  mAdView.getLayoutParams().height = com.google.android.gms.ads.AdSize.SMART_BANNER.getHeightInPixels(getActivity());
  AdRequest adRequest = new AdRequest.Builder()
                   .build();
  mAdView.loadAd(adRequest);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多