0

我运行Code A并得到结果Image A

现在我希望动态加载 Google AD,所以我运行Code B,但我发现 Google AD 无法显示(见图B)。Google AD 的空间似乎隐藏在工具栏后面,请问如何解决?

BTW,如果我把layout_home.xml换成layout_home_modified.xml,好像可以显示Google AD,不知道为什么。

代码 A

class FragmentBuy : Fragment() {

    private lateinit var binding: LayoutBuyBinding

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = DataBindingUtil.inflate(
            inflater, R.layout.layout_buy, container, false
        )
        setHasOptionsMenu(true)
        return binding.root
    }

}

代码 B

class FragmentBuy : Fragment() {

    private lateinit var binding: LayoutBuyBinding

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = DataBindingUtil.inflate(
            inflater, R.layout.layout_buy, container, false
        )
        setHasOptionsMenu(true)

        val adRequest = AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build()
         binding.adView.loadAd(adRequest)

        return binding.root
    }
}

layout_home.xml

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?android:attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways"
                android:background="@color/colorPrimary"
                android:theme="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar"
            />

        </com.google.android.material.appbar.AppBarLayout>

        <fragment
            android:id="@+id/fragment_navigation"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/nav_graph" />


    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</layout>

layout_buy.xml

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:adSize="SMART_BANNER"
            app:adUnitId="@string/ad_unit_id"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
        />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:orientation="vertical"
            app:layout_constraintBottom_toTopOf="@+id/btnPurchase"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/adView">

            <TextView
                android:id="@+id/tvBuy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="10dip"
                android:paddingLeft="7dip"
                android:text="@string/BuyPrompt" />

            <TextView
                android:id="@+id/tvRetrieve"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="25dip"
                android:paddingLeft="7dip"
                android:text="@string/RetrievePrompt" />

        </LinearLayout>

        <Button
            android:id="@+id/btnPurchase"
            style="@style/myTextMedium"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="3dp"
            android:layout_marginTop="3dp"
            android:text="Purchase"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

图像 A

在此处输入图像描述

图像 B

在此处输入图像描述

layout_home_modified.xml

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <com.google.android.material.appbar.AppBarLayout
                android:id="@+id/appbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true">

                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?android:attr/actionBarSize"
                    android:background="@color/colorPrimary"
                    android:theme="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar"
                    app:layout_scrollFlags="scroll|enterAlways" />

            </com.google.android.material.appbar.AppBarLayout>

            <fragment
                android:id="@+id/fragment_navigation"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:defaultNavHost="true"
                app:navGraph="@navigation/nav_graph" />

        </LinearLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</layout>
4

1 回答 1

0

您是否在 AdRequest.Builder() 之前添加了初始化代码?

MobileAds.initialize(this, new OnInitializationCompleteListener() {
     @Override
     public void onInitializationComplete(InitializationStatus initializationStatus) {
     }
 });

如果您正在开发,请使用提供的测试单元 IDca-app-pub-3940256099942544/6300978111

于 2020-07-20T08:49:28.780 回答