如何让广告出现在android的活动中?

如何让广告出现在android的活动中?,android,android-layout,Android,Android Layout,我有一个android活动,其xml布局如下: <RelativeLayout> ... <GridLayout> ... </GridLayout> .... </RelativeLayout> <LinearLayout> ... <android.support.design.widget.CoordinatorLayout> <Linear

我有一个android活动,其xml布局如下:

<RelativeLayout>
 ...
    <GridLayout>    
    ...
    </GridLayout>
 ....
 </RelativeLayout>
<LinearLayout>
    ...
    <android.support.design.widget.CoordinatorLayout>
        <LinearLayout>
        ...
        </LinearLayout>

    </android.support.design.widget.CoordinatorLayout>
    <com.google.android.gms.ads.AdView>
    ....
    </com.google.android.gms.ads.AdView>
<LinearLayout

...
...
....
我想加上广告之类的东西。我尝试了以下设置:

<LinearLayout>
    <RelativeLayout>
     ...
        <GridLayout>    
        ...
        </GridLayout>
     ....
     </RelativeLayout>
    <com.google.android.gms.ads.AdView>
    ...
    </com.google.android.gms.ads.AdView>
</LinearLayout>

...
...
....
...
但它没有显示广告(点代表我认为对这个问题不重要的其他内容)。有什么特殊的技巧可以用来展示广告吗?使布局更加复杂?我必须使用的东西有特殊的布局吗

我的主要活动是展示广告;这里的结构如下:

<RelativeLayout>
 ...
    <GridLayout>    
    ...
    </GridLayout>
 ....
 </RelativeLayout>
<LinearLayout>
    ...
    <android.support.design.widget.CoordinatorLayout>
        <LinearLayout>
        ...
        </LinearLayout>

    </android.support.design.widget.CoordinatorLayout>
    <com.google.android.gms.ads.AdView>
    ....
    </com.google.android.gms.ads.AdView>
<LinearLayout

...
...
....

放弃广告初始化的问题,正如您所说的,它在其他活动中工作,并关注布局问题,我想说问题如下:

您有一个垂直线性布局,覆盖整个屏幕(在顶层匹配父项)和两个孩子

  • 第一个子项是覆盖整个父项的RelativeLayout(匹配父项)
  • 第二个子项是可能正在显示的广告视图,但您无法看到它,因为它从屏幕高度后的第一个像素开始
因此,要纠正这一点,我建议您尝试:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="#cccccc"
    >
    .
    .
    .
</RelativeLayout>

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>


据我所知,您必须使用AdRequest对象在活动代码中初始化您的广告:

    AdView adView = (AdView) findViewById( R.id.adView );
    AdRequest request = new AdRequest.Builder()
            .build();
    adView.loadAd( request );

确保Admob布局显示在xml视图中。将admob视图放入RelativeLayout中,并尝试使用
android:alignparentBottom:true

你可以检查这个答案。

原因可能在于布局的宽度和高度。重新发布你的布局,包括属性“布局宽度”、“布局高度”和“布局重量”。请查看更新后的帖子…这类作品。我有一个空白的按钮(白色),没有任何文字,广告,内容…那么你需要抛光你的布局尺寸一点。您是否尝试在android studio中使用Visual editor?你应该能够预览布局。预览不会显示正在运行的广告,但你可以看到为广告保留的空间,并检查尺寸是否正确,等等。然后在运行时你会看到广告。我不知道到底要检查什么。在我的手机上,我只看到一个空白区域…你能更改adview的布局宽度以匹配父视图吗?如果你仍然看不到广告,但确实看到了它的保留空间,那么启动广告时可能会出现问题。你可以在本页上阅读更多内容:这就是解决方案!!!当然,我怎么会忘记!没有什么比android更复杂的了!!Android几乎和Swisscom一样。不要介意。。。我将您的答案标记为正确…确保您使用的是正确的Admob ID。检查您的日志。