Java 没有显示android项目的广告

Java 没有显示android项目的广告,java,android,Java,Android,我一直在遵循可能已经过时的指示,在我的android项目中插入(测试)广告。在我在虚拟Nexus5手机上重新编译了这个项目之后,这个应用程序就可以工作了,但是没有显示添加横幅 布局文件可能有问题,因为我的布局文件看起来不像示例布局文件。以下是我的版面文件中包含广告横幅的相关部分: <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:an

我一直在遵循可能已经过时的指示,在我的android项目中插入(测试)广告。在我在虚拟Nexus5手机上重新编译了这个项目之后,这个应用程序就可以工作了,但是没有显示添加横幅

布局文件可能有问题,因为我的布局文件看起来不像示例布局文件。以下是我的版面文件中包含广告横幅的相关部分:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity" >

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"/>

    </android.support.design.widget.AppBarLayout>

    <LinearLayout
      ...
      ...


        <com.google.android.gms.ads.AdView
            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="@string/banner_ad_unit_id">
        </com.google.android.gms.ads.AdView>
    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>


我认为你应该使用:

xmlns:ads="http://schemas.android.com/apk/res-auto"
而不是:

xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"

在与OP的讨论中,发现CoordinatorLayout和AdView都位于一个LinearLayout内,CoordinatorLayout将AdView推离屏幕,因为其高度为“匹配父视图”

通过将Coordinator Layout height更改为0dp,weight更改为1,它将增加到AdView上方的剩余空间,而不是占据整个屏幕

android:layout_height="0dp"
android:layout_weight="1"

不,在重新编译并重新启动虚拟Nexus设备后,我仍然没有看到广告…@Alex-Hmm。。。由于您使用的是
坐标布局
,您的
线性布局
是否指定了正确的布局行为,以便它不会隐藏在
AppBarLayout
下?正确的布局行为是什么?我不知道。我是android的初学者…@Alex将
app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior”
添加到你的
LinearLayout
我已经在LinearLayout中了。我完全不知道这意味着什么…你在线性布局中为你的AdView使用RelativeLayout属性。这并不重要,但您可能应该用普通视图布局替换AdView,以检查它是否正确显示。